timer does not work in IE

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • momukhtar
    New Member
    • Jul 2007
    • 5

    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.

    Code:
    function timer() { 
    //debugger; 
       var xmlHttp; 
       try { 
    
          xmlHttp=new XMLHttpRequest(); 
       } 
       catch (e) { 
          // Internet Explorer 
          try { 
             xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); 
           } 
          catch (e) { 
             try { 
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
             } 
             catch (e) { 
                alert("Your browser does not support AJAX!"); 
                return false; 
             } 
           } 
       } 
        
       xmlHttp.onreadystatechange=function() { 
          if(xmlHttp.readyState==4) { 
             document.onlinetestform.timer1.value = xmlHttp.responseText; 
    
           } 
       } 
       xmlHttp.open("GET","time.php?time=06:51:00",true); 
       xmlHttp.send(null); 
       newtime = window.setTimeout("timer();", 1000); 
    }
    [PHP]<?php

    function timeDiff($start Time) {


    $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_second s = substr($tNow, 6, 2);

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

    $tFinishtime = mktime($start_h our+2, $start_min, $start_seconds) ;
    $tCurrenttime = mktime($current _hour, $current_min, $current_second s);

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

    $hoursround = "0".$hoursround ;

    if($secondsroun d < 10)
    $secondsround = "0".$secondsrou nd;
    if($minutesroun d < 10)
    $minutesround = "0".$minutesrou nd;

    echo $hoursround. ":". $minutesround. ":".$secondsrou nd;
    }

    timeDiff($_GET['time']);
    ?>[/PHP]
    Last edited by acoder; Oct 7 '08, 06:56 AM. Reason: Added [code] tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #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.
    Code:
    xmlHttp.open("GET","time.php?time=06:51:00&d="+(new Date()).getTime(),true);

    Comment

    • momukhtar
      New Member
      • Jul 2007
      • 5

      #3
      Originally posted by acoder
      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.
      Code:
      xmlHttp.open("GET","time.php?time=06:51:00&d="+(new Date()).getTime(),true);
      Yup u were right thanks

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Excellent. Glad it's working :)

        Comment

        • clain
          New Member
          • Feb 2007
          • 79

          #5
          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...

          Comment

          Working...