Home  >  Article  >  Web Front-end  >  How to create a delayer in JavaScript

How to create a delayer in JavaScript

青灯夜游
青灯夜游Original
2021-10-15 16:20:4011810browse

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)".

How to create a delayer in JavaScript

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 = &#39;JavaScript&#39;){
            document.write(str + "<br>");
        };
        setTimeout(myFun, 500, &#39;Hello&#39;);
        setTimeout(myFun, 1000);
        setTimeout(function(){
            document.write("定时器<br>");
        }, 1500)
        setTimeout("document.write(\"setTimeout()\")", 2000);
    </script>
</body>
</html>

Rendering:

How to create a delayer in JavaScript

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn