如何将responseText值转换为整数?

时间:2015-12-01 20:46:47

标签: javascript ajax

我有一个PHP脚本来计算两个日期之间的差异。

public class Member {

  private String FirstName;
  private String LastName;
  private int memberId;
  private int Id;
  private String phoneNumber;
  private String member;

  public Member(String FirstName, String LastName, int uniqueId, String phoneNumber) {

    this.Id = uniqueId;
    this.FirstName = FirstName;
    this.LastName = LastName;
    this.phoneNumber = phoneNumber;    
  }

  public void addMember(String FirstName, String LastName, int uniqueId, String phoneNumber) {

  }

  public String getMember() {
    return FirstName + LastName;
  }

  public String getlistOfMembers() {
    return (FirstName + LastName);
  }

  public int getId() {
    return Id;
  }

  public void printMemberId(int Id) {
    if (Id <= 100) {
      System.out.println("Member Id: " + this.Id);
      System.out.println("first name:  " + FirstName);
      System.out.println("LastName:  " + LastName);
      System.out.println("Member phone number:  " + phoneNumber);
    } else
      System.out.println("incorrect");
    ;
  }

  public void getMemberDetails() {
    if (Id <= 100) {
      System.out.println("Member Id: " + Id);
      System.out.println("first name:  " + FirstName);
      System.out.println("LastName:  " + LastName);
      System.out.println("Member phone number:  " + phoneNumber);
    } else
      System.out.println("incorrect");
    ;
  }
}

然后我想用ajax请求每秒自动刷新一次。

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

  $diff=abs(strtotime($date2)-strtotime($date1)); 
  echo "<h1 style=\"color:red;\">".$diff."</h1>";
?>

现在我想计算 totalsec 值来确定分钟数,小时数和天数。但是当我试图对 totalsec 进行任何数学运算时,比如

function loadXMLDoc(){
     var xmlhttp=new XMLHttpRequest();
     xmlhttp.onreadystatechange=function(){
         if (xmlhttp.readyState==4 && xmlhttp.status==200){
             var totalsec=xmlhttp.responseText;
             document.getElementById("ajaxDiv").innerHTML = totalsec;
         }
     }  
     xmlhttp.open("GET","dif_btw_dates.php",true);
     xmlhttp.send();
}
setInterval("loadXMLDoc()",1000);

我收到NaN错误。如何将responseText值转换为整数,以便我可以进行数学运算?感谢。

2 个答案:

答案 0 :(得分:2)

这里的主要问题是您尝试将<h1 style="color:red;">123123</h1>解析为数字。另外,正如@LGSon所指出的,你不应该在这里每秒进行一次ajax调用。

要解决此问题,我会从服务器发回两个日期,它可能看起来像这样:

PHP:

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

HTML:

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

CSS:

#ajaxDiv > h1{
  color: red;
}

和JS:

var date,
    timezoneOffset;

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

function updateTime(){
    if( date === undefined ) return;
    var date2 = new Date();
    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 hour
setInterval(updateTime, 1000);

答案 1 :(得分:0)

var sec = parseInt(xmlhttp.responseText);