Updated April 5, 2023
Introduction to settimeout in Java
In Java, we have wait() or sleep() as time methods and setTimeout which is a javascript method that is defined as a to run a function after the time interval waits, where this method returns a numerical value representing the timer’s ID value by evaluating the given expression for the given specified number of times in milliseconds. In general, the setTimeout() method executes the function only once in contrast to the setInterval() method, and this method has another way that is to write this procedure with the window prefix or without it. Therefore in java, the setTimeout is a function provided in javascript and not in java.
Syntax:
setTimeout(function, milliseconds);
In the above syntax, we can see the setTimeout() function takes in two arguments, and these are as follows :
function – this parameter is a function that contains a block of code or set of logic statements to be executed.
milliseconds – this parameter is used to set the time after which the function is executed.
How setTimeout() works with examples?
In Java, to execute any code after some delay, we use wait or sleep() functions similarly, in javascript we have setTimeout () method, and the time specified within this function will be in milliseconds. When your code is running the setTimeout() method, it will only run once after the delay. Therefore, there is no need to be concerned about the execution of your code several times. There is an optional delay parameter, but you do not have to; you can use it. Let us see why in the article. A call-back function is another parameter accepted by this method. Once the delay is running, the setTimeout() method executes your passed call-back and any content you place inside the call-back function.
Examples:
<html>
<head>
<title> setTimeout() method </title>
</head>
<body>
<h1> Hello World :) :) </h1>
<h3> This is an example of using the setTimeout() method </h3>
<p> Here, an alert dialog box will display after two seconds. </p>
<script>
var a; // initializing variable a
a = setTimeout(fun, 2000); // calling the setTimeout() function
function fun()
{
alert(" Welcome to the code "); // sending an alert on the browser window to
demonstrate setTimeout()
}
</script>
</body>
</html>
Output:
Here in the above-mentioned code snippet, we will see a simple demonstration of setTimeout() function while execution, variable a, and function fun() are defined at the start automatically. This is called hoisting. Now when we call the setTimeout() function and in the parameters, we give the first parameter as a function to be executed after the interval, and the second parameter is the time delay needed in milliseconds. When the code is executed, the program will wait for 2000 milliseconds, i.e. 2 seconds, and then execute the snippet to display a pop-up message on the browser, as shown below in the diagrams.
Example #2
This is another example of the setTimeout() method being used. Here, after 3 seconds, a new tab opens and closes after three seconds. The window is used. Open () new tab and window opening method. Closing () the open tab method.
Because we do not use a way to prevent the function specified in setTimeout() method from being executed. So, after a given time interval, the function only runs once.
Code:
<html>
<head>
<title> setTimeout() method </title>
</head>
<body>
<h1> Hello World :) :) </h1>
<h3> This is an example of using the setTimeout() method </h3>
<p> Here, a new tab will open after 3 seconds and close after 3seconds. </p>
<script>
var a = setTimeout(fun1, 3000);
function fun1()
{
var win1 = window.open();
win1.document.write(" <h2> Welcome to the code </h2>");
setTimeout(function(){win1.close()}, 3000);
}
</script>
</body>
</html>
Output:
Now, here in the above code snippet, we will see how to open a new tab/window on your browser and close it automatically after a delay, hence implementing the setTimeout() function.
As explained in the earlier snippet, we again call the setTimeout() function and pass two parameters, i.e., function fun and the delay time of 3 seconds.
In the fun function, win1 is declared. Open() is a method to open a new browser tab or a new browser window bypassing the specific parameters. On writing win1.document.write(“text”), we are telling the browser to write the passed text on the window.
In the next step, as always, we have written the setTimeout() function again, but this time we did not pass a function; we passed a command/instruction to the browser. After 3 seconds are passed, the browser will execute the command and close the browser window by itself.
We can also stop the execution.
To explain this, we need to get a clear understanding of clearTimeout()function. clearTimeout() function in JavaScript it clears the timeout which has been set by setTimeout() function before that.
After specified time, setTimeout() will run the passed function. The id number returned with the setTimeout() function is stored in the variable to clear the timer.
Given below is a simple code of cleartimeout()
var variable1;
function mytimeFunction() { // taking mytime as a function
variable1 = setTimeout(function () {alert ("Hey World");}, 5000); // given a delay of 5000 miliseconds
}
function myClearFunction() {// myclear as a clearout function
clearTimeout(varaible1);
}
<html>
<head>
<title> setTimeout() method </title>
</head>
<body>
<h1> Hello World :) :) </h1>
<h3> This is an example of using the setTimeout() method </h3>
<p> Click the following button before 3 seconds to see the effect. </p>
<button onclick = "stop()"> Stop </button>
<script>
var a = setTimeout(fun1, 3000);
function fun1()
{
var win1 = window.open();
win1.document.write(" <h2> Welcome to the code</h2>");
setTimeout(function(){win1.close()}, 3000);
}
function stop() {
clearTimeout(a);
}
</script>
</body>
</html>
Output:
The new tab will now open only for 3 seconds, and it will close by itself, and the tab will look like as below.
Conclusion – settimeout Java
In this article, we conclude that in java, there is wait() and sleep() time function, which is similar to setTimeout() method of javascript, which is a built-in method that allows you to time the execution of a certain function where we need to pass the amount of time to wait for, in milliseconds, which mean to wait for one second, you need to pass one thousand milliseconds. To cancel a setTimeout() method from running, you need to use the clearTimeout() method, passing the ID value returned when you call the setTimeout() method.
Recommended Articles
This is a guide to settimeout Java. Here we discuss How setTimeout() works and examples along with the codes and outputs. You may also have a look at the following articles to learn more –