在1秒内重定向setinterval或停止setinterval的条件

redirect in setinterval within 1 sec javascript OR stop setinterval on condition

本文关键字:setinterval 条件 重定向 1秒      更新时间:2023-09-26
setInterval(function () {
//=======  
//code 
//======== 
if(--timer<0){ 
   timer=0;
   window.location = "logout.php?uid=<?php echo $uid ?>&etype=<?php echo $et ?>";
} 
},1000);

计时器结束后,这里,而不是重定向同一页面重新加载一次又一次。

你可以试试这个

var timer = 3;    //your timer length
var myVar = setInterval(function() {
  //code
  if (--timer < 0) {
    clearInterval(myVar);
    window.location = "logout.php?uid=<?php echo $uid ?>&etype=<?php echo $et ?>";
  }
}, 1000);

必须为区间定义一个变量。使用clearInterval 删除定时器

var interval = setInterval(function(){
    if (true) doWhatShouldHappenIfTrue();
    else clearInterval(interval);
}, 1000);