Basically I have a refresh button, when the user clicks the refresh button it calls a web service, gets the data back and refreshes a div container to display the new data.
But I want to update the div container without having the user click the refresh button….
How do I have jQuery click my button every 2 seconds, without refreshing the entire page? or is there a better way to do this?
You could use
setIntervalto run a function every two seconds:And then have the called function call the button’s click handler. Ideally you’d be able to call the click handler directly without have to say
$x.click().Using
setTimeoutmight be a better idea:Then the callback function would call the click handler and, when that finishes, it would call
setTimeoutagain to trigger itself. This approach will keep your timed events from overrunning each other. Since all the code is yours, you can restructure it so that the$.ajaxsuccess and error callbacks (or perhaps thecompletecallback) could restart the timer.