在 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() 方法就会执行您传递的回调以及您放置在回调函数中的任何内容。
示例:
<title> setTimeout() method </title> <h1 id="Hello-World"> Hello World :) :) </h1> <h3 id="This-is-an-example-of-using-the-setTimeout-method"> 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>
输出:
在上面提到的代码片段中,我们将看到 setTimeout() 函数的简单演示,同时执行,变量 a 和函数 fun() 在开始时自动定义。这称为提升。现在,当我们调用setTimeout()函数时,在参数中,我们将第一个参数指定为要在间隔后执行的函数,第二个参数是所需的时间延迟(以毫秒为单位)。当代码执行时,程序将等待 2000 毫秒,即 2 秒,然后执行代码片段以在浏览器上显示弹出消息,如下图所示。
示例#2
这是使用 setTimeout() 方法的另一个示例。此处,3 秒后,打开一个新选项卡,并在三秒后关闭。窗户被使用。 Open()新标签页和窗口打开方法。关闭()打开的选项卡方法。
因为我们没有使用任何方法来阻止setTimeout()方法中指定的函数被执行。因此,在给定的时间间隔后,该函数仅运行一次。
代码:
<title> setTimeout() method </title> <h1 id="Hello-World"> Hello World :) :) </h1> <h3 id="This-is-an-example-of-using-the-setTimeout-method"> 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 "); setTimeout(function(){win1.close()}, 3000); } </script>
输出:
现在,在上面的代码片段中,我们将看到如何在浏览器上打开一个新选项卡/窗口并在延迟后自动关闭它,从而实现 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); } <title> setTimeout() method </title> <h1 id="Hello-World"> Hello World :) :) </h1> <h3 id="This-is-an-example-of-using-the-setTimeout-method"> 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"); setTimeout(function(){win1.close()}, 3000); } function stop() { clearTimeout(a); } </script>
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.
以上是设置超时Java的详细内容。更多信息请关注PHP中文网其他相关文章!

Java在企业级应用中被广泛使用是因为其平台独立性。1)平台独立性通过Java虚拟机(JVM)实现,使代码可在任何支持Java的平台上运行。2)它简化了跨平台部署和开发流程,提供了更大的灵活性和扩展性。3)然而,需注意性能差异和第三方库兼容性,并采用最佳实践如使用纯Java代码和跨平台测试。

JavaplaysigantroleiniotduetoitsplatFormentence.1)itallowscodeTobewrittenOnCeandrunonVariousDevices.2)Java'secosystemprovidesuseusefidesusefidesulylibrariesforiot.3)

ThesolutiontohandlefilepathsacrossWindowsandLinuxinJavaistousePaths.get()fromthejava.nio.filepackage.1)UsePaths.get()withSystem.getProperty("user.dir")andtherelativepathtoconstructthefilepath.2)ConverttheresultingPathobjecttoaFileobjectifne

Java'splatFormIndenceistificantBecapeitAllowSitallowsDevelostWriTecoDeonCeandRunitonAnyPlatFormwithAjvm.this“ writeonce,runanywhere”(era)橱柜橱柜:1)交叉plat formcomplibility cross-platformcombiblesible,enablingDeploymentMentMentMentMentAcrAptAprospOspOspOssCrossDifferentoSswithOssuse; 2)

Java适合开发跨服务器web应用。1)Java的“一次编写,到处运行”哲学使其代码可在任何支持JVM的平台上运行。2)Java拥有丰富的生态系统,包括Spring和Hibernate等工具,简化开发过程。3)Java在性能和安全性方面表现出色,提供高效的内存管理和强大的安全保障。

JVM通过字节码解释、平台无关的API和动态类加载实现Java的WORA特性:1.字节码被解释为机器码,确保跨平台运行;2.标准API抽象操作系统差异;3.类在运行时动态加载,保证一致性。

Java的最新版本通过JVM优化、标准库改进和第三方库支持有效解决平台特定问题。1)JVM优化,如Java11的ZGC提升了垃圾回收性能。2)标准库改进,如Java9的模块系统减少平台相关问题。3)第三方库提供平台优化版本,如OpenCV。

JVM的字节码验证过程包括四个关键步骤:1)检查类文件格式是否符合规范,2)验证字节码指令的有效性和正确性,3)进行数据流分析确保类型安全,4)平衡验证的彻底性与性能。通过这些步骤,JVM确保只有安全、正确的字节码被执行,从而保护程序的完整性和安全性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3汉化版
中文版,非常好用

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。