Ajax%20Interview%20Questions%20and%20Answers
Question: How do I create a thread to do AJAX polling?Answer:JavaScript does not have threads. JavaScript functions are called when an event happens in a page such as the page is loaded, a mouse click, or a form element gains focus. You can create a timer using the setTimeout which takes a function name and time in milliseconds as arguments. You can then loop by calling the same function as can be seen in the JavaScript example below. function checkForMessage() { // start AJAX interaction with processCallback as the callback function } // callback for the request function processCallback() { // do post processing setTimeout("checkForMessage()", 10000); } Notice that the checkForMessage will continue to loop indefinitely. You may want to vary the increment the interval based on activity in the page or your use cases. You may also choose to have logic that would break out of the loop based on some AJAX response processing condition. |
Is it helpful?
Yes
No
Most helpful rated by users:
- What's AJAX ?
- Is it possible to set session variables from javascript?
- Who's Using Ajax ?
- Did Adaptive Path invent Ajax? Did Google? Did Adaptive Path help build Google's Ajax applications?
- What is the difference between proxied and proxyless calls?