Repeat code every 4 seconds

后端 未结 6 2077
遇见更好的自我
遇见更好的自我 2021-01-04 04:07

I want repeat this code every 4 seconds, how i can do it with javascript or jquery easly ? Thanks. :)

$.get(\"request2.php\", function(vystup){
   if (vystup         


        
6条回答
  •  抹茶落季
    2021-01-04 04:33

    Call a Javascript function every 2 second continuously for 20 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");
    }

提交回复
热议问题