js的setInterval(“方法”,时间间隔) 怎么停止

今天写了一个倒计时方法,用的SetInterval(“方法名”,时间间隔)方法,发现进入了死循环,后来百度了下,找到了怎样停止SetInterval方法,原理在于用clearInterval(t); 清除了方法,防止一直循环执行下去

如下:

<script type="text/javascript">        

$(function () {            

var time = new Date();           

var minute = parseInt(time.getMinutes()) + 0;            

var seconds = time.getSeconds() + 10;            

//            window.location.href = "VIPManager/Register.aspx";            

var t = setInterval("showTime(" + minute + "," + seconds + ")", 1000);        

});        

function showTime(minute, seconds) {           

  var t = parseInt(minute - new Date().getMinutes()) * 60 + parseInt(seconds) - parseInt(new Date().getSeconds());           

  if (t - 1 <= 0) {                

   alert('支付时间已过,请重新登录');                

   clearInterval(t);                

  window.location.href = "VIPManager/Register.aspx";          

}            

t = t - 1;            

minute = parseInt(t / 60);            

seconds = t % 60;            

if (minute < 10) {                

minute = "0" + minute;            

}            

if (seconds < 10) {             

    seconds = "0" + seconds;            

}

  $(".spantime").text("00:" + minute + ":" + seconds);        

}

 </script>

posted @ 2014-07-22 18:25  小蚕豆  阅读(5455)  评论(1编辑  收藏  举报