Home > Web Front-end > JS Tutorial > How to use the setTimeout method

How to use the setTimeout method

不言
Release: 2019-01-30 16:26:21
Original
22925 people have browsed it

The setTimeout() method will call the function after the time specified in milliseconds. The setTimeout method requires 2 parameters: a reference to the callback function and a delay in milliseconds. In this article, we will take a look at the setTimeout method. specific usage.

How to use the setTimeout method

Let’s first look at the basic syntax of setTimeout

setTimeout(function, milliseconds, param_one, param_two, ...)
Copy after login

To stop setTimeout and prevent the execution of the function, you need to use the clearTimeout() method.

The setTimeout() method returns an ID that can be used in the clearTimeout() method.

Let’s look at a simple example

The code is as follows

<!DOCTYPE html>
<html>
<body>

<script>
  var sampleVar;
  function sampleFunction(){    
    sampleVar = setTimeout(alertFunc, 2000);
  }
  function alertFunc(){    
    alert("Two seconds have passed!");
  }
  sampleFunction();
</script>

</body>
</html>
Copy after login

The above code will open the pop-up window after 2 seconds.

Example 2

This example will change the text of the element every 2 seconds (3 times). To do this, the ID of some HTML element must be set to "counter".

The code is as follows

<!DOCTYPE html>
<html>
<body>

<p>单击下面的按钮。输入字段将显示经过2、4和6秒。</p>

<button onclick="timedText()">Display timed text</button>

<input type="text" id="text">

<script>
function timedText() {
    var x = document.getElementById("text");
    setTimeout(function(){ x.value="2 seconds" }, 2000);
    setTimeout(function(){ x.value="4 seconds" }, 4000);
    setTimeout(function(){ x.value="6 seconds" }, 6000);
}
</script>

</body>
</html>
Copy after login

The display effect on the browser is as follows

How to use the setTimeout method

When you click the button on the left, the process will be displayed in the text box 2, 4, 6 seconds

This sample will stop "timeout" if "samplestopfunction" is called before the timer runs out.

The code is as follows

<!DOCTYPE html>
<html>
<body>

<p>等待3秒钟后,单击第一个按钮显示“Hi”。</p>
<p>单击第二个按钮以阻止执行第一个函数<br>(必须在3秒钟前单击它)</p>

<button onclick="sampleFunction()">Try it</button>
<button onclick="sampleStopFunction()">Stop the alert</button>

<script>
var sampleVar;

function sampleFunction() {
 sampleVar = setTimeout(function(){ alert("Hi") }, 2000);
}

function sampleStopFunction() {
 clearTimeout(sampleVar);
}
</script>

</body>
</html>
Copy after login

The display effect on the browser is as follows

How to use the setTimeout method

This article ends here, more exciting content You can pay attention to the relevant column tutorials on the PHP Chinese website! ! !

The above is the detailed content of How to use the setTimeout method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template