我有这个功能,使用setTimeout
运行几秒钟。单击按钮时会运行此功能。
function complete() {
var div = document.getElementById('log');
setTimeout(function(){ div.innerHTML = div.innerHTML + intro[Math.floor(Math.random() * intro.length)] + "<br>"; }, 500);
setTimeout(function(){ div.innerHTML = div.innerHTML + second[Math.floor(Math.random() * second.length)] + "<br>"; }, 2560);
setTimeout(function(){ div.innerHTML = div.innerHTML + third[Math.floor(Math.random() * third.length)] + "<br>"; }, 4860);
setTimeout(function(){ div.innerHTML = div.innerHTML + fourth[Math.floor(Math.random() * fourth.length)] + "<br>"; }, 7860);
setTimeout(function(){ div.innerHTML = div.innerHTML + fifth[Math.floor(Math.random() * fifth.length)] + "<br>"; }, 9640);
}
但是,如果用户多次单击该按钮,此功能也会开始多次执行。我试图通过使用下面的代码来防止这种情况发生,但是,它无法正常工作。
var running;
function complete() {
if (running == true) { alert('error'); }
running = true;
var div = document.getElementById('log');
setTimeout(function(){ div.innerHTML = div.innerHTML + intro[Math.floor(Math.random() * intro.length)] + "<br>"; }, 500);
setTimeout(function(){ div.innerHTML = div.innerHTML + second[Math.floor(Math.random() * second.length)] + "<br>"; }, 2560);
setTimeout(function(){ div.innerHTML = div.innerHTML + third[Math.floor(Math.random() * third.length)] + "<br>"; }, 4860);
setTimeout(function(){ div.innerHTML = div.innerHTML + fourth[Math.floor(Math.random() * fourth.length)] + "<br>"; }, 7860);
setTimeout(function(){ div.innerHTML = div.innerHTML + fifth[Math.floor(Math.random() * fifth.length)] + "<br>"; }, 9640);
running = false;
}
我应该采取什么方法,或者如何修复我的代码以便它可以完成我想要做的事情?
答案 0 :(得分:7)
你的running = false;
应该在内部超时功能,因为超时将以异步方式执行,running = false;
将在超时结束前执行
一个简单的例子是
var running = false,
div = document.getElementById('response'),
limit = 5,
current = 0;
$('#trigger').click(function () {
if (running === true) {
alert('Error: The cycle was running. Aborting.');
running = false;
return false;
}
running = true;
var end = setInterval(function () {
if (current >= limit || running == false) {
running = false;
clearInterval(end);
}
div.innerHTML += 'Hello World<br />';
current++;
}, 500);
});
答案 1 :(得分:0)
假设您已使用addEventListener
将click事件附加到按钮,则可以在超时进行时删除侦听器:
function complete() {
var div = document.getElementById('log'),
button = this;
this.removeEventListener('click', complete);
setTimeout(function(){...});
:
setTimeout(function(){...});
// If you later want to make the button clickable again, just uncomment the line below:
// setTimeout(function () {button.addEventListener('click', complete);}, 10000);
}
答案 2 :(得分:0)
停止跑步计时器你必须清除他
var stopTimer;
function someFunction() {
stopTimer= setTimeout(function(){
console.log("something done");
}, 3000);
}
function stopFunction() {
clearTimeout(stopTimer);
}
答案 3 :(得分:-1)
如果您使用的是jquery,那可能会更容易。
E.q:
function complete() {
var div = document.getElementById('log');
setTimeout(function(){ div.innerHTML = div.innerHTML + intro[Math.floor(Math.random() * intro.length)] + "<br>"; }, 500);
setTimeout(function(){ div.innerHTML = div.innerHTML + second[Math.floor(Math.random() * second.length)] + "<br>"; }, 2560);
setTimeout(function(){ div.innerHTML = div.innerHTML + third[Math.floor(Math.random() * third.length)] + "<br>"; }, 4860);
setTimeout(function(){ div.innerHTML = div.innerHTML + fourth[Math.floor(Math.random() * fourth.length)] + "<br>"; }, 7860);
setTimeout(function(){ div.innerHTML = div.innerHTML + fifth[Math.floor(Math.random() * fifth.length)] + "<br>"; }, 9640);
$( "input[type='button']" ).unbind( "click", complete );
}
$( "input[type='button']" ).bind( "click", complete );
我为你做了一些例子: http://jsfiddle.net/ZA5Ga/
祝你好运!