每10秒更新一次的时间轴

Timeline that gets updated every 10 seconds

本文关键字:一次 时间 10秒 更新      更新时间:2023-09-26

我想做一个每隔几秒更新一次的时间轴,不需要刷新页面(它将从服务器上更新的单独文件中获取信息),是否有任何JQuery可以使这变得容易?谁能在网页上给我举个例子?如果需要的话,数据也会以10秒的固定间隔更新。如果可能的话,我想坚持只用CSS3 HTML5和JQuery

非常简单的js,不需要jQuery的"更新每10秒"

// set interval
var interval = setInterval(mycode, 10000);
function mycode() {
  // your ajax request or your function call to fetch data
}
function abortTimer() { // call to stop the timer
  clearInterval(interval );
}

获取更多信息:关于类似问题的伟大帖子

  1. 首先执行$.ajax请求。
  2. 调用refreshTimeline处理您的响应数据并显示successerror .
  3. 等待10秒,重新ajax。

请看下面的例子:

     function request() {
      $.ajax({
        url: "your.php",
        data: {}, //if there data to send
        // recommended to use json and parse on success
        //dataType:"json", 
        success: refreshTimeline, // try to refresh if success
        error: refreshTimeline, // try to refresh if error
       });
     }
    function refreshTimeline(data) {
      //handle data as you like
      $("#time_line").html(data);
      setTimeout(request, 10000); //wait 10 sec and request again
    }
    request(); // start ajax request