Run JavaScript function at regular time interval

前端 未结 4 446
悲哀的现实
悲哀的现实 2020-11-28 13:02

I am currently building a website to host software. What I want is to add to the project pages is a slideshow of screenshots cycling, changing images about every 5 seconds.

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 13:41

    setInterval:

    function doSomething() {
        alert('This pops up every 5 seconds and is annoying!');
    }
    
    setInterval(doSomething, 5000); // Time in milliseconds
    

    Pass it the function you want called repeatedly every n milliseconds. (setTimeout, by the way, will call a function with a timeout.)

    If you ever want to stop the timer, hold onto setInterval’s return value and pass it to clearInterval.

提交回复
热议问题