在javascript中,我需要执行以下伪代码:
REPEAT EVERY 5 SECONDS
print 'One'
wait 2.5 seconds
print 'Two'
wait 2.5 seconds
我该怎么做?我的(笨拙?)尝试如下:
a = setInterval(function() {
setTimeout(function(){print 'one'},0);
setTimeout(function(){print 'two'},2500);
},5000);
改进?
答案 0 :(得分:3)
var counter = 0;
setInterval(function() {
console.log( counter++ % 2 ? 'one' : 'two' );
}, 2500);
答案 1 :(得分:3)
为什么要将setTimeout和setInterval结合起来仍然是个谜,但无论如何:
setInterval(function() {
console.log('one');
setTimeout(function() {
console.log('two');
},2500);
}, 5000);