简体   繁体   English

使用JavaScript经过一定时间后,让函数运行吗?

[英]Make a function run when a certain amount of time has passed with JavaScript?

I want to create a stopwatch with JavaScript, except when a certain amount of time has passed, a function runs, like an alert or something. 我想用JavaScript创建一个秒表,除了经过一定时间后,一个函数会运行,例如警报或其他东西。 The thing that I can't find on the web though, is that I don't want the text to show how much time has passed. 我在网上找不到的是,我不希望文本显示已经过去了多少时间。

I've tried to go into some code and add if statements about when a particular time comes about, the function runs. 我尝试输入一些代码,并添加有关特定时间何时到来的if语句的功能。 But I haven't had any luck so far. 但是到目前为止我还没有运气。

I don't have any example code files or anything like that because I've been testing loads of different ones. 我没有任何示例代码文件或类似的文件,因为我一直在测试不同文件的负载。

So in the end, I don't want the stopwatch to show the text of much time has passed so far. 因此,最后,我不希望秒表显示到目前为止已经花费了很多时间的文本。 I also want a function to run when a particular amount of time has passed after clicking the button. 我还希望在单击按钮后经过特定时间后运行函数。 Hope somebody can help! 希望有人能帮忙! Thank you in advance. 先感谢您。

Use setTimeout() and set the number of milliseconds you want to wait as below: 使用setTimeout()并设置您要等待的毫秒数,如下所示:

 setTimeout(function(){ alert('it works'); }, 2000); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM