471,863 Members | 1,152 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,863 software developers and data experts.

timer does not work in IE

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
4 1836
acoder
16,027 Expert Mod 8TB
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
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
16,027 Expert Mod 8TB
Excellent. Glad it's working :)
Oct 7 '08 #4
clain
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.

Similar topics

3 posts views Thread by redneon | last post: by
8 posts views Thread by Jim Hammond | last post: by
3 posts views Thread by vickey | last post: by
11 posts views Thread by Hotrod2000 | last post: by
8 posts views Thread by Rainer Queck | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by
aboka
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.