Alternative to setInterval()

Hello,

I’m updating a somewhat complicated and very intertwined script that is a page with a bunch of tables with data in them. It’s basically built on using setInterval function to update the data on the page. There is a refresher function that makes cascading ajax calls to function after function. Each function call basically builds a new table with updated data and displays the new table on the main page. There isn’t any issue to me with that inherently except I’m tasked with adding new features to the table, like checkboxes for each row in the table and a modal that can pop up and do different things. The problem I’m facing is that every time the setInterval is called it both clears the checkboxes and removes all of my previously created event handlers. One option is that I can pause the setInterval every time a checkbox is selected or a modal is selected but that creates a different issue which is that if the user checks a checkbox and then forgets about it the data won’t update. In any case I’m wondering if there’s a better option than using the setInterval, I can refactor the page, but I haven’t found another option that might work. If there’s not a better option is there some way to work within the restraints of setInterval that anyone has dealt with before??

Sorry I know some of this is kind of vague and I’m happy to show some code examples if that would be helpful. Thanks for any input and guidance!

Why do you use setInterval?
What is its purpose in refreshing/updating the page?
Are you using it to “wait” for function to return value before you call the next one?

@MyTrueName So each table basically reflects the real time data on users that are using the app. So for example someone using the app might move from place A to place B and that change would be saved to the database. The setInterval function is used to show the updated data being pulled from the database for each user. Am I explaining that clearly?

I’m leaving you in pro’s hands. : )

Hi @camperextraordinaire sorry, so the position didn’t refer to the user’s position in the table but instead GPS coordinates while using the app. There are quite a few metrics we measure related to GPS data as well as data that the user regularly inputs themselves. Those metrics are constantly changing in real time across many users and those changes are being reflected in those tables by refreshing the tables at regular intervals.

Thank you @MyTrueName !

If it’s becoming an issue polling using setInterval, then you can push from the database as well rather than always pulling. I assume it’s sitting on a web server, you can just use server sent events and in the app, listen for those events and update accordingly

1 Like

@DanCouper Ohh interesting, a solution like that might work really well. Thank you for the heads up!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.