How to Interrupt setInterval in Javascript?

Asked

Viewed 1,668 times

1

how do I interrupt the execution of a setInterval that is running on a module that I no longer need? I’m not talking about just catching the feedback from setInterval and execute a clearInterval. Isn’t that...

Example:

app js.

// Rota de data e hora para uma página que exibe em tempo real essa informações para o usuário
const dateTimeSystem  = require('./routes/date-time');
// outros códigos
app.use('/datetime' , dateTimeSystem);

date-time.js

// meus códigos...
//...
//..

setInterval(atualizaDataHora, 1000);

function atualizaDataHora() {
    // meus outros códigos...
}

When I access the route to the time date page, this continuous update is correct and can stay indefinitely while I am on the page. However, when going to any other route/page, I need this "updatedDataHora" function to stop running one at a time, as I no longer need it. I checked in debug mode that it continues to run even after I switch to another page.

How do I interrupt the setInterval in this case?

1 answer

1

"I’m not talking about just taking back the setInterval and running a clearInterval. That’s not it..."

I think that’s exactly what it is. The "return" of the setInterval is a pointer to call the clearInterval that cancels it. This is the only way to stop setInterval.

Another way that you can use, but that does not stop, is to have a flag in the function that gives Return early if it is no longer necessary, but to stop even with:

pointer = setInterval(updateDataHora, 1000);

function atualizaDataHora() {
    // meus outros códigos...

    // e quando já não for mais preciso:
    clearInterval(ponteiro);
}
  • Yes, I agree it’s the clearInterval who has the task of interrupting the execution of the timed routine with the setInterval. What I’m failing to understand is: once the return pointer of the setInterval still inside the archive date-time.js, when switching to another route/page (any route...) I "pick up" this pointer in which way to interrupt the setInterval?

  • @wBB in that case the file date-time.js you have to export the pointer, or a function with it in the scope. And on the router you have to turn it on and off. A question, that setInterval run long asynchronous code?

  • I’m sorry about the beginner question, by the way, I’m confused about many things in Nodejs, but in a "traditional" language I would create a Global variable, for example, which could be accessed from anywhere. I don’t understand how it works at Nodejs... On your question, from the time consuming asynchronous code, do you want to know if it is an extensive code and for this reason it is time consuming to run? If your question is about that, the answer is no, that is, the code is not long, so it is fast running.

  • 1

    @wBB beginner question need no excuse, that’s what the site exists for :) The Node has no global, each file is a module. You can see this question/answer about it. Regarding the code being time-consuming, the idea would be: once it’s not time-consuming, you can’t run that code atualizaDataHora with the request the pending customer and after he run then give the reply to the request res.send(...?

  • Thanks Sergio. I liked the option using Getters and Setters from your answer to the other topic. I think this way will work. About your other solution, responding to the request of res.send after going through the code of atualizaDataHora, checking my code I could not identify how to implement it this way. I have an integration like socket.io, that in turn is integrated with other things, at last... is as confused as it can be to the eyes that are beginning to become acquainted with it. But thanks again for the help!

  • In a preliminary test it worked very well your option indicated in the other topic. I’m just finding it strange that when testing with the codes you suggested, when I click on the browser two requests are executed, Consequently, instead of the "global" counter increment from only 1 to the total, 2 are incremented with each request (which is performed in duplicate). I made a comment there....

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.