【问题标题】:How can I refresh the page dynamically? [duplicate]如何动态刷新页面? [复制]
【发布时间】:2013-01-04 01:29:24
【问题描述】:

可能重复:
Refresh a PHP page for every predefined seconds

我有一些代码,想每 20 秒刷新一次页面。

刷新必须在事件上。我的意思是,如果我加载网页,然后有人把东西放在桌子上,我想每 20 秒显示一次(例如)。

这是链接:http://tuts.wtfdiary.com/2012/07/twitter-style-dynamic-tweet-update.html

【问题讨论】:

  • 查找javascript的setInterval函数
  • 一定要区分刷新页面(可以使用元刷新标签,或者在setTimeout中设置window.location)和刷新其内容(需要 AJAX,在链接页面上有很好的解释)。

标签: php jquery mysql ajax


【解决方案1】:

您可以使用 javascript 的 setInterval()(更具体地说,window.setInterval())以间隔运行函数。像这样:

setInterval(myFunction,20000); //time is in milliseconds, so 20 seconds is 20000

function myFunction() {
  console.log('hi');
  //or whatever else you want to run at an interval.
}

【讨论】:

  • 但是我没有任何功能。我该怎么做?我把它放在哪里?
  • 创建和应用函数是你的工作,因为你没有展示你所拥有的任何东西。
  • 是的,所有代码都在网站上 kentuts.wtfdiary.com/2012/07/…
【解决方案2】:

创建将实现 GET 的 PHP Get 数据和 JavaScript 函数

//Ajax call to get tweets

function GetTweets(){
    $.ajax({
    url: "Get_tweet.php",
    data: /supply parameter if you have any/,
    cache: false,
    success: function(html){
       $("#content").prepend(html);
    }
    });
}

使用 window.setInterval 方法调用函数并根据您指定的时间间隔获取数据

window.setInterval(GetTweets(),20000)

【讨论】:

  • 我现在有这个:function GetTweets(){ $.ajax({ type: "POST", url: "post_tweet.php", data: dataString, cache: false, success: function(html ){ $("#tweet").val(''); $(html).prependTo("#content").fadeIn(500); } }); } }返回假; }); }); window.setInterval(GetTweets(),20000) 不行,怎么回事?
  • 正如我所说,您应该创建 GET。您所拥有的只是一个仅用于发布的 POST。创建它的对应物,即 GET 以获取数据
猜你喜欢
  • 1970-01-01
  • 2013-09-16
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2013-02-23
  • 2014-05-23
  • 2012-05-20
  • 1970-01-01
相关资源
最近更新 更多