I have a googlemap on a form. When the user pans/zooms I make a server call to fetch the markers to show on the page based on the boundary.
The problem is that the server call takes a second or two to complete but the user has continued to zoom/pan causing another event to fire.
I’d like to debounce the event so it only fetches after the boundary has settled for a half second or so.
I found a python decorator routine that would work in python but requires threading which is not yet implemented in Skulpt.
Any idea how I can achieve this?
Just thinking quickly here but could you record a timestamp each time your pan/zoom event occurs, as well as call a function that sleeps for some duration prior to executing the server function. If the timestamp of the event occurs within your sleep duration, abort the server call (since an event happened recently).
I’ve never used the map component directly in Anvil and so I may be misunderstanding the question.
I usually use a timer to simulate threads (old VB6 trick).
Instead of calling the server function directly, you can set a timer to 0.5 seconds, then call the server function from the timer event. This alone ensures that two server calls don’t happen closer than 0.5 seconds. Then you can add some logic inside the timer event handler to make other decisions.
This worked reasonably well. Getting the last event to fire correctly took some fiddling so here’s the finished result for others (and future me).
https://anvil.works/build#clone:OTO34AGGQB4G74YY=5C4KSGHJQDQLP4KE4SCOZ6JR
1 Like
Your version seems to be starting the slow task without waiting for the previous run to be done, and it gets confused on the number of runs.
Here is a version that uses the timer and always waits for the previous run to be finished: https://anvil.works/build#clone:PBNH54HQ2DYZRQLF=25V55NUPIZGCZ4NZ4EQKFASZ
I noticed it too after I posted and was working around it another way but your solution is much cleaner than my hack. Thank you!