討論 » 開發

repeat executung script

§
發表於:2015-09-23

repeat executung script

hi, for example i have a ajax request, how i can repeat the request every 1 minute? thank you.

woxxom管理員
§
發表於:2015-09-23
編輯:2015-09-23
  • Use setInterval to automatically call the specified function periodically:

    var interval = setInterval(function() {
       // do ajax request
       ...............
    }, 60 * 1000);
    
    if (some_condition) {
        clearInterval(interval); // stop the timer
    }
    
  • Use setTimeout to schedule one run of the function based on condition:

    var timeout;
    (function doAjax() {
       // do ajax request
       ...............
       if (some_condition) {
           timeout = setTimeout(doAjax, 60 * 1000); // schedule the next run
       }
    })();
    
    if (another_condition) {
        clearTimeout(timeout); // cancel the schedule
    }
    
§
發表於:2015-09-23

thank you wOxxOm!, you are good for a teacher :D

發表回覆

登入以回復