0

I want to start timer after x interval seconds.This x interval seconds will come from server side.

Now i have some process going on server side and on page load i call getData method which starts a timer based on isRunning flag and i display progress untill i found isRunning false and then stop a timer.

So if user is already on my page where i have 1 schedule jobs which will run after x seconds so i want this getData to call after that x seconds.Right now once user comes on the page getData keeps on going server side for getting progress for schedule jobs.

Hence i was thinking that i will get total seconds of first schedule job and configure my timer to start getData method after x seconds if user doenst refresh the page.

But here i am not getting how to configure $scope.startTimer method in such a way that it will start after x seconds.For eg:If schedule job will run after an hour then start timer method should only start after 1 hour and keep calling getData method every 2 seconds.

Below is my code:

function getData() {
        myService.get(function (response) {
             if ($scope.isRunning)
                 $scope.startTimer();
             else
                 $scope.stopTimer();
             //response.totalSeconds;here i will get total seconds after which i want to call this method again to show progress of schedule job
        });
    }


var stop;
    $scope.startTimer = function () {
        // Don't start a new timer
        if (angular.isDefined(stop)) return;

        stop = $interval(function () {
            if ($scope.refreshTimer > 0) {
                $scope.timerLabel = false;
                $scope.refreshTimer = $scope.refreshTimer - 1;
            } else {    //REFERSH CALL
                $scope.stopTimer();
                getMockProgress();
            }
        }, 2000);
    };

    $scope.stopTimer = function () {
        if (angular.isDefined(stop)) {
            $interval.cancel(stop);
            stop = undefined;
        }
    };

Update :

else
    $scope.stopTimer();
    if (response.milliSeconds > 0)
              setTimeout($scope.startTimer, response.milliSeconds);

But this fails to call startTimer method after x milliseconds.

I will appreciate any help :)

I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
  • try using [setTimeout()](https://developer.mozilla.org/ro/docs/Web/API/window.setTimeout) – Alexandre Miziara Apr 17 '18 at 12:40
  • @AlexandreMiziara I have to return seconds or milliseconds from server side for setTimeout function?Right now i am returning seconds – I Love Stackoverflow Apr 17 '18 at 12:42
  • milliseconds, like it is explained in the docs. Take a look at the example, it is pretty simple to use :) – Alexandre Miziara Apr 17 '18 at 12:43
  • @AlexandreMiziara I used setTimeout method but it isnt working.See my update – I Love Stackoverflow Apr 17 '18 at 13:16
  • 2
    Use $timeout not setTimeout! This is AngularJs people use the tools it provides you. Check out this [article](https://stackoverflow.com/questions/39310992/difference-between-settimeout-in-javascript-and-timeout-service-in-angularjs) on the differences in using the two. – Mickers Apr 17 '18 at 13:29
  • @Mickers is right. If you are using this inside an angular controller, you might need to substitute `setTimeout()` with `$timeout`. – Alexandre Miziara Apr 17 '18 at 13:32

0 Answers0