Как остановить setinterval javascript

от admin

How to cancel a setInterval in Javascript

During the development of my degree final project, I had the necessity of cancel a previously setInterval in Javascript. To anyone who doesn’t know what this is, a setInterval operation allows the programmer set a period in which the task is executed.

Searching a bit on the Internet I found a post in StackOverflow that shows various possible options to cancel (or simulate a cancellation) of a setInterval operation, but the most correct one could be something like this:

This clearInterval(interval) function is part of the basic JavaScript standard, implemented in most browsers and avoids the execution of the interval operation again.

There are other methods using flags, but these doesn’t stop the interval forever (just returns from the operation without doing anything) so this seems a more correct way to do it.

How to exit from setInterval

I need to exit from a running interval if the conditions are correct:

3 Answers 3

Pass the value of setInterval to clearInterval.

The timer is decremented every second, until reaching 0.

Updated for ES6

You can scope the variable to avoid polluting the namespace:

rovyko's user avatar

    The Overflow Blog
Linked
Related
Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.3.11.43304

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Как остановить SetInterval в секундомере?

Я написал секундомер, который начинает работать при нажатии на цифру 1, но я не могу остановить его работу, когда пользователь дошёл до последнего (суть игры простая: нужно по порядку нажать на все цифры) 5e60e8fd8f92e528116564.png

Читать:
Как добавить пиво в untappd

Сам секундомер работает через setInterval, который вызывается отдельной функцией и я не могу его остановить из другой функции

Stop setInterval call in JavaScript

I am using setInterval(fname, 10000); to call a function every 10 seconds in JavaScript. Is it possible to stop calling it on some event?

I want the user to be able to stop the repeated refresh of data.

Kamil Kiełczewski's user avatar

7 Answers 7

setInterval() returns an interval ID, which you can pass to clearInterval() :

its4zahoor's user avatar

If you set the return value of setInterval to a variable, you can use clearInterval to stop it.

Hatchet's user avatar

You can set a new variable and have it incremented by ++ (count up one) every time it runs, then I use a conditional statement to end it:

I hope that it helps and it is right.

Already answered. But if you need a featured, re-usable timer that also supports multiple tasks on different intervals, you can use my TaskTimer (for Node and browser).

In your case, when users click for disturbing the data-refresh; you can also call timer.pause() then timer.resume() if they need to re-enable.

Onur Yıldırım's user avatar

In nodeJS you can you use the "this" special keyword within the setInterval function.

You can use this this keyword to clearInterval, and here is an example:

When you print the value of this special keyword within the function you output a Timeout object Timeout

assayag.org's user avatar

The Trick

setInterval returns a number:

enter image description here

Solution

Take this number. Pass it to the function clearInterval and you’re safe:

enter image description here

Code:

Always store the returned number of setInterval in a variable, so that you can stop the interval later on:

(Think of this number as the ID of a setInterval . Even if you have called many setInterval , you can still stop anyone of them by using the proper ID.)

Похожие статьи