We help IT Professionals succeed at work.

javascript ajax works in firefox but intermittent in ie8

ewan69
ewan69 asked
on
456 Views
Last Modified: 2012-08-13
Hi, i fire a request every 3 seconds using setTimeout it runs o.k. in forefox but in ie it stops after a minute or 2, time varies and it doesn't throw any errors.

some of the code is below, any ideas??
function getXMLHttpRequest() {
      if (window.XMLHttpRequest) {
         return new XMLHttpRequest();
      } else if (window.ActiveXObject) { 
         try {
            return new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               return new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      return null;
} 

function getAjaxContent(url, callback)
{
	var ajax_req = getXMLHttpRequest();
	ajax_req.onreadystatechange = callback;
	ajax_req.open('GET',url,true);
        ajax_req.send(null);
} 
function vehUpdate()
{
	if(this.readyState==4)
	{
        var currentTime = new Date()
	var hours = currentTime.getHours()
	var minutes = currentTime.getMinutes()
	if (minutes < 10)
	{
		minutes = "0" + minutes
	}
	var seconds = currentTime.getSeconds()
	if (seconds < 10)
	{
		seconds = "0" + seconds
	}
	document.getElementById('lastUpdated').innerHTML="&nbsp Time:"+hours+":"+minutes+":"+seconds;
        setTimeout("getUpdate()",3000);
        }
} 
function getUpdate()
{
	getAjaxContent("getUpdate.php?type=updates", vehUpdate);
}

Open in new window

Comment
Watch Question

IT Expert
CERTIFIED EXPERT
Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:

Author

Commented:
Hi, tried moving this line

ajax_req = getXMLHttpRequest();
into my init function and added var ajax_req=null; at the start, just trying it...
Top Expert 2009

Commented:
always use time stamp as it might get cached values

also you could change  

instead of doing setTimeout("getUpdate()",3000);

you could kick in the concept of setInterval

Author

Commented:
the reason for not using setInterval is that i want it to run with a gap of 3 seconds between finishing it and running again, setInterval doesn't do that...
I will do the timstamp thing, thanks.

Author

Commented:
Brilliant, works fine now thanks...
Top Expert 2009

Commented:
>> You get a new xmlhttprequest each time

Getting a new request each time did not work? that's strange.
also you did not try to have the time stamp?

well good it solved!

Good day.

Author

Commented:
Getting a new request each time did not work? that's strange.

It works fine on firefox, running for days no memory leaks or issues.
IE only runs 100-500 hundred times before stopping.

I add timestamp now but it was definatly reusing the object that solved the problem...
Top Expert 2009

Commented:
all right
thanks.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.