Calling a function every 60 seconds

后端 未结 13 2085
情书的邮戳
情书的邮戳 2020-11-22 08:24

Using setTimeout() it is possible to launch a function at a specified time:

setTimeout(function, 60000);

But what if I would l

13条回答
  •  不思量自难忘°
    2020-11-22 09:07

    Call a Javascript function every 2 second continuously for 10 second.

    var intervalPromise;
    $scope.startTimer = function(fn, delay, timeoutTime) {
        intervalPromise = $interval(function() {
            fn();
            var currentTime = new Date().getTime() - $scope.startTime;
            if (currentTime > timeoutTime){
                $interval.cancel(intervalPromise);
              }                  
        }, delay);
    };
    
    $scope.startTimer(hello, 2000, 10000);
    
    hello(){
      console.log("hello");
    }

提交回复
热议问题