setTimeOut() is not working with AJAX [duplicate]

一个人想着一个人 提交于 2019-12-04 19:26:57

You have a couple of issues in this code segment that are creating your bugs:

window.onload = show_data('Dev');

A bit of explanation about why window.onload = show_data('Dev'); doesn't work is probably in order: window.onload needs to be a function. As your code executes, it is evaluating show_data('Dev') (which doen't return a value, but does start an XMLHTTPRequest, so it appears to work) and executing window.onload = undefined;.

window.onload=show_data; would work - but then you don't get your parameter passed.

Luckily JavaScript allows you to create anonymous functions - leading to:

window.onload = function() { show_data('Dev'); };

setTimeOut(show_data('Dev'), 10000);

First of all, JavaScript is a case sensitive language. setTimeOut !== setTimeout. Also the first parameter to setTimeout/setInterval is expected to be a function, which suffers from the same problem here as your window.onload did, it calls setTimeout(undefined, 10000);, but executes the request immediately. I also think you mean to call it with the same strName as it was called with.

Your timeout set should say:

setTimeout(function() {show_data(strName);}, 10000); 

A side note - setInterval() and setTimeout() both allow passing strings that get eval()ed at runtime, but I would suggest against using that method which looks like this:

// please dont use me
setTimeout("show_data(strname)", 10000);

At this point your code should work when edited with those two lines. The rest of this stuff is just other optimizations

setTimeout() vs. setInterval()

This seems like it is going to keep checking every 10000ms, unless the ajax request fails, then it will stop. I imagine you just wanted to poll forever. setInterval() allows you to setup a "repeating" timer.

Remove your setTimeout line and replace window.onload with:

var updateInterval;
window.onload = function(){ 
  updateInterval = setInterval(function(){ show_data('Dev'); }, 10000);
};

// an example - lets stop polling 35 seconds from now
setTimeout(function() {
 clearInterval(updateInterval);
}, 35000);
rahul

Replace

window.onload = show_data('Dev');

with

window.onload = function() {
   var intervalID = setInterval( function(){ show_data('Dev')}, 10000);
}

and remove

setTimeOut(show_data('Dev'), 10000);

from stateChangeHandler function.

and don't forget to clear the interval

using

clearInterval(intervalID);

The difference between setInterval and setTimeout is that, setTimeout() triggers expression only once, whereas setInterval() keeps triggering expression again and again (unless you tell it to stop).

instead of:

setTimeOut(show_data('Dev'), 10000);

could/have you tried:

setTimeOut(function() { show_data('Dev') }, 10000);

?

Your window.onload method is definitely wrong. replace that with this:

window.onload = function(){var intervalID = setInterval(function(){show_data('Dev')}, 10000);}

Other answers here seem to cover the other problems so I won't touch that. cheers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!