Skip to content Skip to sidebar Skip to footer

How To Convert Responsetext Value To An Integer?

I have a php script that compute the difference between two dates.

Solution 1:

The main problem here is that you are trying to parse <h1 style="color:red;">123123</h1> into a number. Also, as pointed out by @LGSon, you shouldn't make an ajax call every second here.

To resolve this I would send back the two dates from the server instead, it could look something like this:

PHP:

<?php$date1="2015-11-30 07:57:00";
  $date2=date('Y-m-d H:i:s');
  echo$date1 . "," . $date2;
?>

HTML:

<divid="ajaxDiv"><h1></h1></div>

CSS:

#ajaxDiv > h1{
  color: red;
}

and the JS:

var date,
    timezoneOffset;

functionloadXMLDoc(){
     var xmlhttp=newXMLHttpRequest();
     xmlhttp.onreadystatechange=function(){
         if (xmlhttp.readyState==4 && xmlhttp.status==200){
             var dates = xmlhttp.responseText.split(",");
             timezoneOffset = Math.round((newDate() - newDate(dates[1]))/3600000);
             date = newDate(dates[0]);
         }
     }  
     xmlhttp.open("GET","dif_btw_dates.php",true);
     xmlhttp.send();
}

functionupdateTime(){
    if( date === undefined ) return;
    var date2 = newDate();
    date2.setHours(date2.getHours() - timezoneOffset);
    var totalsec = (date2 - date) / 1000;

    /* Here totalsec is a number and you can do your other operations like 
       var minutes=totalsec /60; */var ajaxDiv = document.getElementById("ajaxDiv");
    var h1 = ajaxDiv.getElementsByTagName("h1")[0];  
    h1.innerHTML = totalsec;

}

loadXMLDoc();
setInterval(loadXMLDoc, 3600000); //Sync with server time once an hoursetInterval(updateTime, 1000);

Solution 2:

var sec = parseInt(xmlhttp.responseText);

Post a Comment for "How To Convert Responsetext Value To An Integer?"