By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,741 Members | 2,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,741 IT Pros & Developers. It's quick & easy.

setTimeout in IE

P: 6
I'm trying to write an AJAX application which periodically updated some data, here's the outline of my code, cut down to the important bits:

Expand|Select|Wrap|Line Numbers
  1. ...
  2.  
  3. var http = createRequestObject();
  4.  
  5. function createRequestObject() {
  6.     var ro;
  7.     var browser = navigator.appName;
  8.     if (browser == "Microsoft Internet Explorer") {
  9.         ro = new ActiveXObject("Microsoft.XMLHTTP");
  10.     } else {
  11.         ro = new XMLHttpRequest();
  12.     }
  13.     return ro;
  14. }
  15.  
  16. function startup() {
  17.     // Do Stuff
  18.     update();
  19. }
  20.  
  21. function update() {
  22.     http.open('get', 'test.php');
  23.     http.onreadystatechange = updated;
  24.     http.send(null);
  25. }
  26.  
  27. function updated() {
  28.     if (http.readyState == 4) {
  29.         // Do More Stuff
  30.     }
  31.     setTimeout(update, 500);
  32. }
  33.  
  34. ...
  35.  
  36. <body onload="startup();">
  37.  
  38. ...
My problem is that on IE, the setTimeout event never fires, so update is called once when called by startup(), but then never again. On other browsers it works fine. I've tried replacing setTimeout(update, 500); with setTimeout('update()', 500); but that didn't work either.

Can anyone suggest what I'm doing wrong?
Oct 24 '08 #1
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
It'll probably be caused by caching. To confirm, add an alert to your update() function.

PS. there's no need for browser detection in your createRequestObject function.
Oct 24 '08 #2

P: 6
yes it was casheing, thanks, i have it fixed now.
Oct 24 '08 #3

acoder
Expert Mod 15k+
P: 16,027
No problem. Glad you got it to work :)
Oct 24 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.