Repeating setTimeout

前端 未结 7 879
太阳男子
太阳男子 2020-12-15 15:56

I am trying to repeat setTimeout every 10 seconds. I know that setTimeout by default only waits and then performs an action one time. How can I rep

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-15 16:24

    Using jQuery, this is what you could do:

    function updatePage() {
    
    var interval = setTimeout(updatePage, 10000); // 10' Seconds
    
        $('a[href]').click(function() {
          $(this).data('clicked', true);
          clearInterval(interval); // Clears Upon Clicking any href Link
          console.log('Interval Cleared!');
        });
       // REPLACE 'YOUR_FUNCTION_NAME' function you would like to execute
        setTimeout(YOUR_FUNCTION_NAME, 500);
    
    } // Function updatePage close syntax
    
    updatePage(); // call the function again.

提交回复
热议问题