In JavaScript, you can use the setTimeout() method to set the delay for a few seconds. This method is used to call a function or calculate an expression after a specified number of milliseconds. The syntax is "setTimeout(code to be executed, waiting for milliseconds)".
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
setTimeout() is a method belonging to window. This method is used to call a function or calculate an expression after a specified number of milliseconds.
The syntax format can be the following two:
setTimeout(要执行的代码, 等待的毫秒数) setTimeout(JavaScript 函数, 等待的毫秒数)
The example is as follows:
setTimeout("alert('这是3秒后出现的')", 3000 )
The output result after 3 seconds:
The example is as follows:
<html> <head> <script type="text/javascript"> function timedMsg() { var t=setTimeout("alert('5 seconds!')",5000) } </script> </head> <body> <form> <input type="button" value="显示计时的消息框!" onClick = "timedMsg()"> </form> <p>点击上面的按钮。5 秒后会显示一个消息框。</p> </body> </html>
Output result:
##Output result after 5 seconds of clicking the button: Related recommendations:The above is the detailed content of How to set delay seconds in javascript. For more information, please follow other related articles on the PHP Chinese website!