我需要有人告訴我如何取消或清除此功能上的定時器。如何從此取消setTimeout?
//Html
<a id="button" data-url="http://url.com">Click me!</a>
Redirecting in <span id="timer"></span> seconds...
<a href="javascript:void(0)" class="cancel">Cancel</a>
// jS
$('a#button').click(function() {
var url = $(this).attr("data-url");
if(url.indexOf("http://")!==0){
url = "http://"+url;
}
var seconds = 5,
el = $('#timer')
el.text(seconds)
setTimeout(function countdown() {
seconds--
el.text(seconds)
if (seconds > 0) {
setTimeout(countdown, 1000)
}
else {
window.open(url , "_self")
}
}, 1000)
})
$('a.cancel').click(function(){
clearTimeout(countdown);
});
還告訴我,我的「M做錯了,這是爲什麼不工作
不走運的傢伙,這裏http://jsfiddle.net/hVTPq/嘗試 – neek