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, it will be executed after the set number of seconds.
Experimental code (change body background color):
Copy the code code 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 (experiment in seconds):
Copy the code code as follows:
<div></div>
<script>
var num = 0;
setInterval(function(){$(".clock").html(num++)},1000);
</script>
Summarize:
The setTimeout() method executes the function after waiting for the specified time, and only executes the passed handle function once.
The setInterval() method executes the passed handle function once every specified interval, and executes it in a loop until the window is closed or clearInterval().