Interrupt setInterval for a function to run in its place?

Asked

Viewed 67 times

2

I have a code that makes a query in the database (every 1 second) using the setInterval and updates a dashboard (Via AJAX and PHP) from my system, which shows the summary of all user tasks. (To give the impression to the user that it is in real time and he sees the panel change when he changes something in the system).

PROBLEM: The problem is that I have a filter and I would like that, when selecting certain companies, the panel only showed the activities of a certain company (and not all, as is done by default). Only this doesn’t happen, because the setInverval keeps running all the time.

Below I will show a system image:

inserir a descrição da imagem aqui

I know the description was kind of generic, but if you could give me some light, I’d appreciate it.

Thank you!

  • 1

    The setInterval() returns the ID of the created timer. To stop it just use this ID as argument for the function clearInterval.

  • Suddenly, inside the setInterval, let the function check what is in the client field, and update the search with the value that the user selects. To be more precise, I’d have to see the code.

1 answer

2


This is what @fernandosavio said. setInterval() needs an identifier, for example:

timer = setInterval(function(){

   //executa ações

}, 1000);

To stop it, use the identifier assigned to the setInterval():

clearInterval(timer);

However, it is not recommended to use setInterval to make AJAX requests, as explained in this answer. It’s right to use setTimeout() calling a function that calls AJAX after it is processed. By using setInterval() for this purpose you can stress (overload) the server and browser, as explained in the above-mentioned answer.

Browser other questions tagged

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