Settimeout method with parameters in js

  • 2020-03-30 02:11:57
  • OfStack

Js settimeout method plus the use of parameters.
Easy to use look at w3school
There's no argument call in there,
Example:

<script type="text/javascript"> 
function timedMsg() 
{ 
var a ="dd"; 
var t=setTimeout(function(){ cao(a);},3000) 
} 
function cao(a) 
{ 
alert(a); 
} 
</script> 
</head> 
<input type="button" value=" Displays a timed warning box " onClick = "timedMsg()"> 

My problem now is between multiple, only the last one comes back. Think about it

/* for (var i=0;i<points.length;i++){ 
AddGuiJi(points[i],value); 
//var t=setTimeout(function(){ AddGuiJi(points[i],value);},2000*i) 
} */ 

The reason for the error in the above is that when time is triggered, I =4, so it is multiple but always the last one
The solution

var timerr; 
function xunhuanlian(points,value){ 
if(replayi<points.length){ 
AddGuiJi(points[replayi],value); 
replayi++; 
timerr=setTimeout(function(){ xunhuanlian(points,value);},1000); 
} 
else{ 
replayi =0; 
clearTimeout(timerr); 
} 
/* for (var i=0;i<points.length;i++){ 
AddGuiJi(points[i],value); 
//var t=setTimeout(function(){ AddGuiJi(points[i],value);},2000*i) 
} */ 
} 

Related articles: