简体   繁体   English

使用ajax进行实时网站更新? jQuery的?

[英]Live site updating with ajax? JQuery?

There was a site 'happybirthdadstevejobs.com' where people wished Steve happy birthday and when you did you were added and the site was being continually updated with other wishing Steve a happy birthday too. 有一个网站“ happybirthdadstevejobs.com”,那里的人们希望史蒂夫生日快乐,而当您添加您时,该站点正在不断更新,其他人也希望史蒂夫生日快乐。

I want to figure this out as I have an idea to teach kids some science concepts. 我想解决这个问题,因为我有一个想法要教孩子一些科学概念。 Can anyone help me understand what I need to do? 谁能帮助我了解我需要做什么?

You can use jquery 你可以使用jQuery

var i = setInterval(function() {
   $.getJSON("inc/messages.php?"+ params, function(data) {
      $('#updates').html(data.messages);
   }
}, 1000);

So that will get some messages from messages.php and output them to the element with id 'updates' every second. 这样就可以从messages.php获取一些消息,并将它们每秒输出到ID为'updates'的元素。

To stop the updates, you can call: 要停止更新,您可以致电:

clearInterval(i)

However, there is probably a more efficient way to do it if the file is going to be called many times. 但是,如果要多次调用该文件,可能有一种更有效的方法。

Edit: forgot to mention that messages.php needs to grab the data from a database or file :) 编辑:忘记提及messages.php需要从数据库或文件中获取数据:)

Try googling Long Polling. 尝试谷歌搜索长轮询。

Node.js is a good option for long polling. Node.js是长时间轮询的一个不错的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM