Skip to content Skip to sidebar Skip to footer

How Can I Make A Function Execute After 2 Minutes And Then At 2 Minute Intervals After That?

I would like to call a function saveData($scope.data) I understand there is an interval function in javascript but how can I make an initial interval of two minutes and then have t

Solution 1:

window.setTimeout(function(){
    window.setInterval(function(){
        saveData($scope.data);
    },2*60*1000);
    saveData($scope.data);
},2*60*1000);

online demo (with interval shorten to highlight effect)

Solution 2:

The following function will execute saveData after every 2 minutes.

setInterval(function(){
            saveData(data)
            },
            2*60*1000);

Solution 3:

You can use

setTimeout("javascript function",milliseconds)

function for one time delay. Then use

setInterval("javascript function",milliseconds)

for specific time delay.

Solution 4:

JS does not have a sleep function, you can implement your own function, like this

functionsleep(milliseconds) {
  var start = newDate().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((newDate().getTime() - start) > milliseconds){
      break;
    }
  }
}

then call from your main function with your desire seconds

Post a Comment for "How Can I Make A Function Execute After 2 Minutes And Then At 2 Minute Intervals After That?"