Home  >  Article  >  Web Front-end  >  Introduction to the difference between setTimeout() and setInterval() methods_javascript skills

Introduction to the difference between setTimeout() and setInterval() methods_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:07:241336browse

Timers setTimeout() and setInterval() are both js timing functions. There are some differences between them.

setTimeout():

Explanation in the js manual: Used to call a function or calculate an expression after a specified number of milliseconds;

That is to say, after execution Execute after the set number of seconds.

Experimental code (change body background color):

Copy code The code is as follows:

setTimeout(function(){
$("body").css("background","red");
},5000);

setInterval() :

Explanation in the js manual: Call a function or calculate an expression according to the specified period (in milliseconds). The function will be called continuously until clearInterval() is called or the window is closed;

Execute your own effect code or function within the number of seconds you set.

Experimental code (several seconds experiment):
Copy code The code is as follows:


<script> <br>var num = 0; <br>setInterval(function(){$(".clock").html( num )},1000); <br></script>

Summary:

The setTimeout() method executes the function after waiting for the specified time, and only executes the transfer once The incoming handle function. The

setInterval() method executes the incoming handle function after every specified interval, and executes in a loop until the window is closed or clearInterval().
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