Jump to content

Issue with setTimeout and IE


tomfmason

Recommended Posts

I have a simple function that gets the time from a php script.. I have the timeout set to 500 so that it will give it the appearance of a clock..

Well this works fine in FF and Opera.

But in IE it will only work if I don't set a time out. In which case it will not reload the time..

Here is the function in question..

[code]
function sendRequest() {
http.open('GET', 'process.php?action=getTime');
http.send(null);
http.onreadystatechange = handleRequest;
}

function handleRequest() {
if (http.readyState == 4) {
clearInterval(dtimer);
var response = http.responseText;
document.getElementById('time').innerHTML = response;
var browser = navigator.appName;
if (browser == 'Microsoft Internet Explorer') {
    dtimer = setTimeout('sendRequest();', 500);
}else{
    dtimer = setTimeout('sendRequest();', 500);
}
}
}[/code]

here is a live link.. http://www.thomashostings.net/home/layout/layout2/

The part in question is in the upper right hand corner.

Any suggestions would be great.

Thanks,
Tom
Link to comment
https://forums.phpfreaks.com/topic/26116-issue-with-settimeout-and-ie/
Share on other sites

I'm not sure why you're using the colon after the function call, but maybe this example that I wrote will help:

index.php
[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>ajax example</title>
    <scri pt type="text/javascript" src="update.js"></s cript>
</head>
<body style="text-align:center;">
    <div id="dump">
        STUFF
        <!-- ajax -->
    </div>
   
    <input type="button" name="start" id="start" value="Start Update" onclick="update();" />
</body>
</html>[/code]

backend.php:
[code]<?php
echo date("n/j/Y h:i:s A");
?>[/code]

update.js:
[code]function createRequestObject()
{
    if (window.XMLHttpRequest) { // Mozilla, Safari, Opera...
        var xmlhttp = new XMLHttpRequest();
        if (xmlhttp.overrideMimeType)
xmlhttp.overrideMimeType('text/xml');
    }
else if (window.ActiveXObject)
{ // IE
        try {
            var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!xmlhttp) {
        alert('Giving up. Cannot create an XMLHTTP instance');
        return false;
    }
return xmlhttp;
}

/* You can get more specific with version information by using parseInt(navigator.appVersion) Which will extract an integer value containing the version of the browser being used.  The variable http will hold our new XMLHttpRequest object. */
var http = createRequestObject();

function update()
{
    http.abort();
    http.open('post', 'backend.php');
    http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http.send('act=1');
    http.onreadystatechange = handleDump;
    setTimeout("update()",1000);
}

function handleDump()
{
if(http.readyState == 4) //Finished loading the response
{
var response = http.responseText;
document.getElementById('dump').innerHTML = response;
}
}
[/code]

Works in IE, FF, Opera.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.