简体   繁体   中英

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.

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

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.

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 :)

Try googling Long Polling.

Node.js is a good option for long polling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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