Having some trouble with my timer and function for retrieving data on button clicks. I'm trying to prevent too many rapid requests.
Here's the code snippet for the timer:
var timer;
And the function that updates via AJAX:
function doTheThing(data){
clearTimeout(timer);
$.ajax({
url:'/post/url',
data:data,
}).done(function(data){
timer = setTimeout(function(){
getMoreData();
},2000);
});
}
The goal is for the timer to reset on each request, preventing multiple simultaneous requests triggered by fast button presses. The getMoreData() function should ideally only fire once even if buttons are pressed rapidly.
function getMoreData(){
// Another AJAX request here
}
But it seems like the timer isn't working as intended. Any ideas on what could be causing this issue?