2013-06-19 184 views
0

我希望我的倒计时停止点击提交按钮,我搜索了一些页面, 但我没有找到任何东西。 这里是我想停止点击停止倒计时

function countDown (count) { 
    if (count > 0) { 
    var d = document.getElementById("countDiv"); 
    d.innerHTML = count; 
    setTimeout (function() { countDown(count-1); }, 1000); 
    document.getElementById('tiempo').value = count; 
    } 
    else 
    document.location = "timeover.php"; 
} 
document.getElementById("palabra").focus(); 
countDown(5); 
</script> 
+0

你的代码是做什么的?你期望它做什么呢?提交按钮在哪里?你的问题在哪里? – Teemu

回答

0

尝试是这样的代码:

var stopped = false; 
function countDown (count) { 
    if (count > 0) { 
    var d = document.getElementById("countDiv"); 
    d.innerHTML = count; 
    document.getElementById('tiempo').value = count; 
    if (!stopped) { 
    setTimeout (function() { countDown(count-1); }, 1000); 
    } 
    } 
    else 
    document.location = "timeover.php"; 
} 
document.getElementById("palabra").focus(); 
document.getElementById("mySubmitId").onclick = function() { 
    stopped = true; 
}; 
countDown(5); 
+0

“count”的递减量在哪里? )? – Teemu

+0

@Teemu但倒计时应该停止。 –

+1

我喜欢它,但似乎OP想要显示元素中的剩余时间('count')。在你的代码中,这个“显示”保持在'4'。 – Teemu

0

你必须保存参照超时(实际上返回超时值将是数)并用它来取消超时。

var timeout = window.setTimeout(function() { 
    // do something 
}, 1000); 

// clear timeout 
window.clearTimeout(timeout); 

你可能有想法。顺便说一句,你应该看看setInterval方法,因为在这种情况下它会更好。间隔将“打勾”,直到您取消为止。