Jump to content

setInterval calling AJAX not working in IE


woolyg

Recommended Posts

This could possibly be grouped with this post: http://www.phpfreaks.com/forums/index.php?topic=327454.0 - the code works in FF and Chrome, etc, but not IE, and I'm wondering if anyone has seen a workaround.

 

Basically, the page is loading a setInterval() on the fly to dynamically update a user's credit left in their account, and show it within a constantly updating div. Every 10 seconds, it runs an ajax call to a PHP script, and the info comes through. It works with everything but IE.

 

I know IE deals with ajax differently that the other browsers, etc, but does it have a distinct problem with setInterval()?

 

 

Here's the code being used:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Updater test</title>
<script language="JavaScript" type="text/javascript">
function ajax_refresh(dataSource, divID) {
               var XMLHttpRequestObject = false;

               if (window.XMLHttpRequest) {
                       XMLHttpRequestObject = new XMLHttpRequest();
                       } else if (window.ActiveXObject) {
                       XMLHttpRequestObject = new
                       ActiveXObject("Microsoft.XMLHttp");
                       }


               if(XMLHttpRequestObject) {
               var obj = document.getElementById(divID);
		   //var starting_data = obj.innerHTML;
		   document.getElementById(divID).innerHTML = obj.innerHTML;

                       XMLHttpRequestObject.open("GET", dataSource, true);
                       XMLHttpRequestObject.onreadystatechange = function()
                       {

                               if (XMLHttpRequestObject.readyState == 4 &&
                                       XMLHttpRequestObject.status == 200) {
                                       obj.innerHTML = XMLHttpRequestObject.responseText;
                                       delete XMLHttpRequestObject;
                                       XMLHttpRequestObject = null;
                                       }
                       }
               XMLHttpRequestObject.send(null);
               }
       }
// STANDARD AJAX CALL END
</script>

</head>

<body>


<script type="text/javascript">
var refresher_update_user_credit_left_div = 0;
refresher_update_user_credit_left_div = setInterval("update_user_credit_left_div('<?php echo $user_id; ?>')", 10000);		

function update_user_credit_left_div($user_id){ // FUNCTION 48				
	ajax_refresh('<?php echo base_url(); ?>assets/ajax/ajax.update_user_credit_left_div.inc.php?uid=<?php echo $user_id; ?>', 'user_credit_left');
} // FUNCTION 48	

</script>


<div id="user_credit_left" title="Your account credit."></div>

</body>
</html>

 

 

 

All input appreciated.

 

Regards

WoolyG

Link to comment
Share on other sites

OK, having delved a bit deeper, it was because IE was caching the ajax call.

 

 

Once I changed:

function update_user_credit_left_div($user_id){ // FUNCTION 48						
           ajax_refresh('<?php echo base_url(); ?>assets/ajax/ajax.update_user_credit_left_div.inc.php?uid=<?php echo $user_id; ?>', 'user_credit_left');	
} // FUNCTION 48	

 

to:

function update_user_credit_left_div($user_id){ // FUNCTION 48						
           ajax_refresh('<?php echo base_url(); ?>assets/ajax/ajax.update_user_credit_left_div.inc.php?uid=<?php echo $user_id; ?>&t=' + (new Date()).getTime(),  'user_credit_left');	
} // FUNCTION 48	

 

 

.. the div began updating every 10 seconds as expected!

 

Solved. Hopefully this can help someone else out some time.

 

WoolyG

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.