Javascript每300秒运行一次脚本

Javascript run script every 300 seconds

本文关键字:一次 脚本 300秒 运行 Javascript      更新时间:2023-09-26

我有以下代码,在我的页面上显示一个php文件。但我希望有人能帮我,这样代码每300秒刷新一次

httpRequest("recent-widget.php", showrecent);
function showrecent(WIDGET){
 d = document.getElementById('recent-widget');
 d.innerHTML = WIDGET;
}
function httpRequest(url, callback) {
  var httpObj = false;
  if (typeof XMLHttpRequest != 'undefined') {
    httpObj = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try{
      httpObj = new ActiveXObject('Msxml2.XMLHTTP');
    } catch(e) {
      try{
        httpObj = new ActiveXObject('iMicrosoft.XMLHTTP');
      } catch(e) {}
    }
  }
  if (!httpObj) return;
  httpObj.onreadystatechange = function() {
    if (httpObj.readyState == 4) { // when request is complete
      callback(httpObj.responseText);
    }
  };
  httpObj.open('GET', url, true);
  httpObj.send(null);
}

只需使用setInterval每隔300000毫秒重复您在顶部进行的调用。例如

setInterval(function() {
   httpRequest("recent-widget.php", showrecent);
}, 300000);

你可以做:

setInterval(function() {
    httpRequest("recent-widget.php", showrecent)
} , 300000);