js setInterval problem
大家讲道理
大家讲道理 2017-05-19 10:44:25
0
4
613

There are two functions a() and b(). I want to execute a first, then execute b after three seconds, then execute a after three seconds, and then execute b after three seconds, and it goes on like this.. How should I write it?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(4)
刘奇
function a() {
    setTimeout(b, 3000);
    console.info('This is a!');
}
function b() {
    setTimeout(a, 3000);
    console.info('This is b!');
}
a();
Ty80
function start() {
    a()
}
var timer;
function a() {
    clearTimeout(timer);
    timer = setTimeout(b, 3000);
}
function b() {
    clearTimeout(timer);
    timer = setTimeout(a, 3000);
}
習慣沉默

a and b are executed every six seconds, a is executed first, and b is executed every three seconds. . . Otherwise, if the function execution time is taken into account, the three seconds interval is not the exact three seconds

为情所困
var flag = true;
var s = setInterval(function(){
    if(flag){
        a();
        
    }else{
        b()
    }
    flag =!flag

},3000)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!