In JavaScript, you can use the setTimeout() function to set a delayer, which is used to call a function or calculate an expression after a specified number of milliseconds, with the syntax "setTimeout(code,millisec)".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
In JavaScript, you can use the setTimeout() function to set a delayer.
The setTimeout() function is used to execute certain codes after a specified time (in milliseconds). The code will only be executed once.
Syntax format:
setTimeout(code,millisec)
Parameter description:
code: required. The string of JavaScript code to be executed after the function to be called. Can be required for a function
#millisec. The number of milliseconds to wait before executing code.
Code example:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>JavaScript</title> </head> <body> <script type="text/javascript"> var myFun = function (str = 'JavaScript'){ document.write(str + "<br>"); }; setTimeout(myFun, 500, 'Hello'); setTimeout(myFun, 1000); setTimeout(function(){ document.write("定时器<br>"); }, 1500) setTimeout("document.write(\"setTimeout()\")", 2000); </script> </body> </html>
Rendering:
[Recommended learning: JavaScript advanced tutorial】
The above is the detailed content of How to create a delayer in JavaScript. For more information, please follow other related articles on the PHP Chinese website!