dongyun3805 2015-04-29 01:41 采纳率: 0%
浏览 44

Ajax不在网站上工作

For my personal site I want to insert a simple ajax server clock into my website, but for some reason it's not showing up in the header. Here's the Javascript code

var httpxml;
try {
  // Firefox, Opera 8.0+, Safari
  httpxml=new XMLHttpRequest();
} catch (e) {
  // Internet Explorer
  try {
    httpxml=new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      httpxml=new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
      alert("ur browser doesn't support ajax m8, try reinstalling windows 95");
      return false;
    }
  } 
}

function stateck() {
  if(httpxml.readyState==4) {
    document.getElementById("time").innerHTML=httpxml.responseText;
  }
}

var url="ajax-server-clock-demock.php";
url=url+"?sid="+Math.random();
httpxml.onreadystatechange=stateck;
httpxml.open("GET",url,true);
httpxml.send(null);
tt=timer_function();
}

function timer_function(){
  var refresh=1000; // Refresh rate in milli seconds
  mytime=setTimeout('AjaxFunction();',refresh)
}

And in php file

<?Php
echo date("d/m/y : H:i:s", time());
?>

Lastly in the header

<time>bacon</time>
  • 写回答

1条回答 默认 最新

  • dtnbjjq51949 2015-04-29 01:58
    关注

    I would have to see more code in context because several things can make this not work, in the mean time, there are multiple issues with your script, among others:

    You did setTimeout(). That would only execute once, assuming you want the clock to update every second you need to use:

    http://www.w3schools.com/jsref/met_win_setinterval.asp

    Oh, also, you need to do something like:

    document.getElementsByTagName('time')[0]; 
    

    Instead of getElementById()

    OR add an id to your element like:

    <time id="mytime"></time>
    

    And then you can call:

    document.getElementById('mytime');
    

    got it?

    And also, where is declared this AjaxFunction() you are calling in setTimeout?

    Also, there are much better ways to do a clock... and you should really consider using jQuery or something like that, will make your life much easier when doing AJAX, DOM manipulation and etc...

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用