Home > Article > Web Front-end > js timer
js can use two timers, one is setInterval (function(){}, time); the other is setTimeout (function(){}, time);
The difference between the two is that setInterval is not set after time milliseconds. Execute a function once, settimeout is executed after time milliseconds,
I encountered a problem here,
Js code
function startFlushTime(min){
var sec = min*60;
var time = timeFormat( sec);
$("#time").html(time);
setInterval(doflush(), 1000);
}
function doflush(){
debugger
var time = $(" #time").html();
if(time && time!=''){
var res = timeFormat(timeParse(time)-1)
$("#time").html(res) ;
}
}
After writing this way, the scheduled task will not be executed and modified to
Js code
function startFlushTime(min){
var sec = min*60;
var time = timeFormat(sec);
$("#time").html(time);
setInterval(function(){doflush();}, 1000);
}
function doflush(){
debugger
var time = $("#time").html();
if(time && time!=''){
var res = timeFormat(timeParse(time)-1)
$("#time").html (res);
}
}
After success, that is, create a new function after setinterval and execute the method that needs to be executed in it