How does JavaScript implement a timed execution of a delay method?

there is such a requirement:
< div id= "demo" > < / div >

This element appears when the

page enters, then disappears after 5 seconds, appears after an interval of 8 seconds, and disappears again after 5 seconds, repeating this process.
my idea is this:

setInterval(function(){
    $("-sharpdemo").css("display","block")
    setTimeout(function(){
        $("-sharpdemo").css("display","none");
    },5000);
}, 8000);

but there seems to be a problem with time. What caused it?


8s |-| setInterval
5s |-| setTimeout
the effect you write should be that the 5s element disappears and the element appears after 3s. In fact, you only need to change the interval between setInterval to 13000.


function delay() {
    setTimeout(function () {
        $('-sharpdemo').css('display','none');
        setTimeout(function () {
            $('-sharpdemo').css('display','block');
            delay();
        }, 8000);
    }, 5000)
}
delay();

change 8000 to 13000


isn't it better to implement


const a = function (){
    setTimeout(function(){
        console.log('a');
        b()
    },1000);
}
const b = function (){
    setTimeout(function(){
        console.log('b');
        a()
    },2000);
}

a()

take a look.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3350a-2abcb.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3350a-2abcb.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?