Jquery 如何从中取消设置超时?

Jquery 如何从中取消设置超时?,jquery,settimeout,Jquery,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

我需要有人告诉我如何取消或清除这个功能的计时器

//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);
    });
//Html
//jS
$('a#按钮')。单击(函数(){
var url=$(this.attr(“数据url”);
if(url.indexOf(“http:/”)!==0){
url=“http://”+url;
}
var秒=5,
el=$(“#计时器”)
el.文本(秒)
setTimeout(函数倒计时(){
秒--
el.文本(秒)
如果(秒>0){
设置超时(倒计时,1000)
}
否则{
窗口。打开(url,“\u self”)
}
}, 1000)
})
$('a.cancel')。单击(函数(){
清除超时(倒计时);
});
另外,请告诉我我做错了什么,以及为什么这样做不起作用。

您需要这样做:

  var myTime;
    $('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)


myTime = setTimeout(function countdown() {
        seconds--
        el.text(seconds)
        if (seconds > 0) {
            myTime =setTimeout(countdown, 1000)
        }
        else {
            //window.open( url , "_self" )
            alert('no');
        }

}, 500);

})


$('a.cancel').click(function(){
        clearTimeout(myTime);

});

告诉
setTimeout
要清除的内容:

countdown = setTimeout(function countdown() {...}
确保脚本顶部宣布了
倒计时
,以便它在
单击处理程序中可用。

添加:

var myTime;

myTime = setTimeout(function countdown() {...
要清除它,请执行以下操作:

clearTimeout(myTime);

一种很好的方法是:

{
var myTime;
myTime = setTimeout(function countdown() {
//blah
alert('Test');
clearTimeout(myTime);
}, 500);
}
然后,您的变量只需确定范围。

我会这样做: (编辑:在此处选中它,它可以正常工作)


但是它会保留主函数吗?实际上让我试试。这在点击处理程序中不起作用,你需要删除
var
keyword是的,把它从点击功能中删除不走运的家伙,在这里试试jsfiddle.net/hVTPq不走运的家伙,在这里试试不走运的家伙,在这里试试jsfiddle.net/hVTPq不走运的家伙,在这里试试jsfiddle.net/hVTPq运气不好的家伙,在这里试试jsfiddle.net/hVTPq运气不好的家伙,在这里试试jsfiddle.net/hVTPq
var timer = null; //this will be used to store the timer object
var seconds = 5;
var url = null;

function countdown() {
    seconds--;

    el.text(seconds);
    if (seconds > 0) {
        timer = setTimeout(countdown, 1000)
    }
    else {
        window.open( url , "_self" )
    }
}

$('a#button').click(function() {
    url = $(this).attr("data-url");

    if( url.indexOf("http://")!==0 ){
        url = "http://"+url;
    }        
    el = $('#timer');
    el.text(seconds)
    timer = setTimeout(countdown, 1000)
})


$('a.cancel').click(function(){
    clearTimeout(timer);
});​