0

In my application development, I am using

setInterval(function() {
  // some code
  // runs here
}, 60000);

I want to execute some code on 1 minute interval and my code may take 2-3 minutes in some cases.

<execute code> - <wait 1 minute> - <execute code> - <wait 1 minute> ......so on

I tried with setInterval function but noticed that setInterval does not wait for inner code to complete. Please suggest how can i achieve this in javascript.

Thanks

5
  • I want to execute some code on 1 minute interval and my code may take 2-3 minutes So 1 min interval or 2 or 3? Commented Apr 18, 2020 at 21:24
  • If your code might take longer than the interval, then setTimeout might be a better option so you not start the next iteration before the first has completed. Commented Apr 18, 2020 at 21:26
  • Why does your code takes so much time do you have promises in it ? Commented Apr 18, 2020 at 21:28
  • Some warnings: if your code needs to do something at 1 minute intervals, you really don't want to rely on setInteverl or setTimeout becuase your users will lose interest and move on to different tabs and that will heavily deprioritize your tab and cause your interval basically be completely invalidated. What do you actually want to do? I.e. the interval is a means to an end, what actually happens once a minute? Is the one minute mark important? Because it probably isn't: what should your user(s) see when they come back to your tab? Commented Apr 18, 2020 at 21:37
  • I am calculating summary data from raw and want to show on dashboard page. 1 minute i am putting as wait time between consecutive iterations else system may crash. Commented Apr 18, 2020 at 21:40

3 Answers 3

2

A better way may be to recursively call your job function with setTimeout:

setTimeout(function jobThatRunEveryMinute() {
  // run code here that may take more than one minute
  someExpensiveCodeToRun()

  // start another job after someExpensiveCode completes
  setTimeout(jobThatRunEveryMinute, 60000);
}, 60000);
Sign up to request clarification or add additional context in comments.

Comments

0

Something like this might work for you, but why do you have a function that takes over 1 min to return something?

let callCompleted = true;

setInterval(function() {
   if (callCompleted) {
      callCompleted = false;

      // pass a call back function to update the callCompleted to true
      myLongRunningFunction(function() { callCompleted = true } );

      // or if `myLongRunningFunction` returns a promise 
      myLongRunningFunction.then(() => { callCompleted = true } );
   } 
}, 60000);

Comments

0
// adjust wait time to your liking
const waitTime = 10000 
let time = 0
// so you can stop it
const totalTime = 6 

function excuteCode() {
  time++;
  console.log(time);  // <=== replace with your inner code
  if (time < totalTime) {
    setTimeout(() => {
      excuteCode()
    }, waitTime)
  }
}

excuteCode();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.