添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm working on some code that'd I'd like to have each loop run every 60 seconds, but currently each loop runs immediately. The purpose of the code it to see if a form has changed, and if it has save the form. Do I have setInterval setup incorrectly?

function saveHelper(formId) {
    for(var i = 0; i < 4; i++) {
        save(formId);
function save(formId) {
    console.log('might save');
    var changed = formChanges(formId);
    var intId = setInterval(stall, 60000);
    if(changed.length > 0) {
        console.log('would save');
        //document.getElementById(formId).submit();
    clearInterval(intId);
function stall() {
    return true;
                Why are you clearing the interval right after you make it? Interval runs the code INSIDE the function you call.... it does not pause the execution of the code.
– epascarello
                Aug 30, 2018 at 13:55
                I would have answered something like espascarello or Nicholas Tower have answered so i just add a link to the documentation of the setInterval() function :) setInterval() - Web APIs | MDN
– Chocolord
                Aug 30, 2018 at 14:13

You are treating interval as some sort of synchronous sleep method, which is not the case. The change code should be inside of the setInterval, it should not live after the interval.

var intId = setInterval(function () {
    if(changed.length > 0) {
        console.log('would save');
        //document.getElementById(formId).submit();
}, 60000);

setInterval doesn't pause your code, it just schedules some code to be run some time in the future. For example, when you do this:

var intId = setInterval(stall, 60000);

That says "every 60000 milliseconds, run the function stall". As soon as this line of code completes, it will immediately run your next line of code, do the saving, then clear the interval. Clearing the interval cancels it, so now nothing will happen in 60000 milliseconds.

Instead, you'll want to do something like this:

function saveHelper(formId) {
  let count = 0;
  const intId = setInterval(function () {
    if(changed.length > 0) {
      console.log('would save');
      //document.getElementById(formId).submit();
    count++;
    if (count === 4) {
      clearInterval(intId);
  }, 60000);

Every 60000 milliseconds, the inner function will run, and do the saving. After saving, it checks how many times we've done this, and once it reaches 4, it clears the interval to stop it from happening any more.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.