You can pass the time in milliseconds to this. setTimeout() accepts a function to execute as its first argument and the millisecond delay defined as a number as the second argument. It is the name of a callback function that contains the code you want to execute. functionname is the first required parameter. That means, if you want to initiate (for example) four ajax requests, then perform an action when they are done, you could do something like this: $.when(ajax1(), ajax2(), ajax3(), ajax4()). If you want to run a javascript function every 5 seconds then you can use the setInterval() method of javascript. The general syntax for the setTimeout () method looks like this: setTimeout (functionname, time) Let's break it down: setTimeout () is a method used for creating timing events. After the call to setTimeout()the script continues normally, with the timer running in the background. It accepts any number of Deferred objects as arguments, and executes a function when all of them resolve. Click the button below and wait 5 seconds: It’s worth pointing out that setTimeout()doesn’t halt the execution of the script during the timeout period it merely schedules the specified expression to be run at the specified time. As such, you may find yourself with queued up XHR requests that won't necessarily return in order. JQuery now defines a when function for this purpose. For example, if using setInterval () to poll a remote server every 5 seconds, network latency, an unresponsive server, and a host of other issues could prevent the request from completing in its allotted time.
0 Comments
Leave a Reply. |