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

timer does not work in IE

P: 5
I have a Ajax / PHP timer code that would run and update timer on a webpage for 2 hrs. But in IE the time does not refresh and maintain its initial value.

Expand|Select|Wrap|Line Numbers
  1. function timer() { 
  2. //debugger; 
  3.    var xmlHttp; 
  4.    try { 
  5.  
  6.       xmlHttp=new XMLHttpRequest(); 
  7.    } 
  8.    catch (e) { 
  9.       // Internet Explorer 
  10.       try { 
  11.          xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); 
  12.        } 
  13.       catch (e) { 
  14.          try { 
  15.             xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
  16.          } 
  17.          catch (e) { 
  18.             alert("Your browser does not support AJAX!"); 
  19.             return false; 
  20.          } 
  21.        } 
  22.    } 
  23.  
  24.    xmlHttp.onreadystatechange=function() { 
  25.       if(xmlHttp.readyState==4) { 
  26.          document.onlinetestform.timer1.value = xmlHttp.responseText; 
  27.  
  28.        } 
  29.    } 
  30.    xmlHttp.open("GET","time.php?time=06:51:00",true); 
  31.    xmlHttp.send(null); 
  32.    newtime = window.setTimeout("timer();", 1000); 
  33. }
  34.  
[PHP]<?php

function timeDiff($startTime) {


$tNow = date("h:i:s");
//echo $tNow."<br/>";

// Extract $current_date h:m:ss
$current_hour = substr($tNow,0,2);
$current_min = substr($tNow,3,2);
$current_seconds = substr($tNow, 6, 2);

$start_hour = substr($startTime,0,2);
$start_min = substr($startTime,3,2);
$start_seconds = substr($startTime, 6, 2);

$tFinishtime = mktime($start_hour+2, $start_min, $start_seconds);
$tCurrenttime = mktime($current_hour, $current_min, $current_seconds);

$hoursround = floor(($tFinishtime-$tCurrenttime)/3600);
$minutesround = floor((($tFinishtime-$tCurrenttime)/60) - (60 * $hoursround) );
$secondsround = ($tFinishtime-$tCurrenttime) - (60 * 60 * $hoursround) - (60 * $minutesround);

$hoursround = "0".$hoursround;

if($secondsround < 10)
$secondsround = "0".$secondsround;
if($minutesround < 10)
$minutesround = "0".$minutesround;

echo $hoursround. ":". $minutesround. ":".$secondsround;
}

timeDiff($_GET['time']);
?>[/PHP]
Oct 7 '08 #1
Share this Question
Share on Google+
4 Replies


acoder
Expert Mod 15k+
P: 16,027
Most likely this problem is caused by caching. To prevent it, either disable caching by setting the appropriate headers in the PHP page, or just add a unique timestamp to the end of the URL, e.g.
Expand|Select|Wrap|Line Numbers
  1. xmlHttp.open("GET","time.php?time=06:51:00&d="+(new Date()).getTime(),true);
Oct 7 '08 #2

P: 5
Most likely this problem is caused by caching. To prevent it, either disable caching by setting the appropriate headers in the PHP page, or just add a unique timestamp to the end of the URL, e.g.
Expand|Select|Wrap|Line Numbers
  1. xmlHttp.open("GET","time.php?time=06:51:00&d="+(new Date()).getTime(),true);
Yup u were right thanks
Oct 7 '08 #3

acoder
Expert Mod 15k+
P: 16,027
Excellent. Glad it's working :)
Oct 7 '08 #4

clain
P: 79
JFYI :
The root cause for this is because of the fact that IE caches the page .. since we use the XMLHTTP object.... so a time stamp fixes this...
Oct 9 '08 #5

Post your reply

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