首页  >  文章  >  Java  >  设置超时Java

设置超时Java

PHPz
PHPz原创
2024-08-30 15:41:48959浏览

在 Java 中,我们有 wait() 或 sleep() 作为时间方法,而 setTimeout 是一个 JavaScript 方法,定义为在时间间隔等待后运行一个函数,该方法返回一个代表计时器时间的数值。通过对给定表达式求值给定的指定次数(以毫秒为单位)来获得 ID 值。一般来说,与 setInterval() 方法相比,setTimeout() 方法只执行一次该函数,并且此方法还有另一种方法,即使用或不使用窗口前缀来编写此过程。因此在java中,setTimeout是javascript中提供的函数,而不是java中提供的函数。

广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试

语法:

setTimeout(function, milliseconds);

在上面的语法中,我们可以看到 setTimeout() 函数接受两个参数,如下所示:

函数 – 该参数是一个函数,包含要执行的代码块或一组逻辑语句。

毫秒 – 该参数用于设置函数执行的时间。

setTimeout() 如何使用示例?

在Java中,要在一段延迟后执行任何代码,我们类似地使用wait或sleep()函数,在javascript中我们有setTimeout()方法,并且在该函数中指定的时间将以毫秒为单位。当您的代码运行 setTimeout() 方法时,它只会在延迟后运行一次。因此,无需担心您的代码会多次执行。有一个可选的延迟参数,但您不必这样做;你可以使用它。让我们在文章中看看原因。回调函数是该方法接受的另一个参数。一旦延迟运行,setTimeout() 方法就会执行您传递的回调以及您放置在回调函数中的任何内容。

示例:

<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>

输出:

设置超时Java

设置超时Java

在上面提到的代码片段中,我们将看到 setTimeout() 函数的简单演示,同时执行,变量 a 和函数 fun() 在开始时自动定义。这称为提升。现在,当我们调用setTimeout()函数时,在参数中,我们将第一个参数指定为要在间隔后执行的函数,第二个参数是所需的时间延迟(以毫秒为单位)。当代码执行时,程序将等待 2000 毫秒,即 2 秒,然后执行代码片段以在浏览器上显示弹出消息,如下图所示。

示例#2

这是使用 setTimeout() 方法的另一个示例。此处,3 秒后,打开一个新选项卡,并在三秒后关闭。窗户被使用。 Open()新标签页和窗口打开方法。关闭()打开的选项卡方法。

因为我们没有使用任何方法来阻止setTimeout()方法中指定的函数被执行。因此,在给定的时间间隔后,该函数仅运行一次。

代码:

<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>

输出:

设置超时Java

设置超时Java

设置超时Java

现在,在上面的代码片段中,我们将看到如何在浏览器上打开一个新选项卡/窗口并在延迟后自动关闭它,从而实现 setTimeout() 函数。

如前面代码片段所述,我们再次调用 setTimeout() 函数并传递两个参数,即函数 fun 和 3 秒的延迟时间。

在 fun 函数中,声明了 win1。 Open() 是一种绕过特定参数打开新浏览器选项卡或新浏览器窗口的方法。在写入 win1.document.write(“text”) 时,我们告诉浏览器在窗口上写入传递的文本。

下一步,一如既往,我们再次编写了setTimeout()函数,但是这次我们没有传递函数;我们向浏览器传递了命令/指令。 3秒后,浏览器将执行命令并自行关闭浏览器窗口。

我们也可以停止执行。

为了解释这一点,我们需要清楚地了解clearTimeout()函数。 JavaScript 中的clearTimeout() 函数会清除之前通过setTimeout() 函数设置的超时。

指定时间后,setTimeout() 将运行传递的函数。 setTimeout() 函数返回的 id 号存储在变量中以清除计时器。

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:

设置超时Java

设置超时Java

设置超时Java

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.

以上是设置超时Java的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn