0

Hello i have a button that when clicked posts some data to my server, the data is then processed after which it returns a result.

What i want to do now is after the response is returned with no errors i want a timer to start after 5secs, when the timer starts it should execute a function after every 1sec. This should go on for a full minute after which the timer should be destroyed and a function should be executed to alert the user that the operation has timed out.

I know how to execute a normal timer using $interval or $timeout but i have no idea how to do what i have described above.

1 Answer 1

2

How about something like this ?

var startTimer = function () {
    // This executes functionToExecuteEverySecond every second
    var interval = $interval(function () {
        functionToExecuteEverySecond();
    }, 1000);

    // This destroys the interval promise after 60 seconds
    $timeout(function () {
        $interval.cancel(interval);
        notifyUser();
    }, 60000);
};

// This executes startTimer function after 5 seconds
$timeout(function () {
    startTimer();
}, 5000);
Sign up to request clarification or add additional context in comments.

2 Comments

Wow this really looks like what i need, i will try it out :)
Tried it! Works exactly how i wanted it to! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.