Introduction and application of js setTimeout of function take countdown as an example

  • 2020-03-30 00:53:05
  • OfStack

Definition and usage:

The setTimeout() method is used to call a function or evaluate an expression after a specified number of milliseconds.

Grammar:

SetTimeout (code, millisec)

Parameters:

Code (required) : a string of JavaScript code to be executed after the function to be called.
Millisec (required) : number of milliseconds to wait before executing the code.

Tip:

SetTimeout () executes code only once. If you want to make multiple calls, use setInterval() or have the code itself call setTimeout() again.

For example:
 
<script language="javascript"> 
function timer(){ 

var value=Number(document.all['time'].value); 
if (value>1) document.all['time'].value=value-1; 
else { 
document.all['time'].value=" Agreed to "; 
return false; 
} 
window.setTimeout("timer()",1000); 

} 
</script> 
<body onload="timer()"> 
<input name="time" value="10" type="button" style="width:40px;" /> 
</body> 

Related articles: