0

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 that function repeatedly executed after that? I also need to save the new $scope.data each time

Sobin Augustine
  • 3,639
  • 2
  • 25
  • 43

4 Answers4

2
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)

Passerby
  • 9,715
  • 2
  • 33
  • 50
0

The following function will execute saveData after every 2 minutes.

setInterval(function(){
            saveData(data)
            },
            2*60*1000);
Sorter
  • 9,704
  • 6
  • 64
  • 74
0

You can use

setTimeout("javascript function",milliseconds)

function for one time delay. Then use

setInterval("javascript function",milliseconds)

for specific time delay.

Ali
  • 752
  • 6
  • 18
0

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

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

then call from your main function with your desire seconds

Sobin Augustine
  • 3,639
  • 2
  • 25
  • 43