我如何像聊天室一样用JQuery循环一个mysql查询? [关闭]

I want to keep checking a mysql query for rows in the database the same way a chatroom constantly queries and loads new messages. In effect, checking and loading new messages as they are entered into the database.

Also, any tips on how to add a sound effect when new queries are retrieved? Many Thanks

What you're looking for is a repeating, timed poll. You can do this with javascript's setTimeout() method.

As far as playing a sound, I can't help you there.

If whenever you want to an action to be performed when data is inserted into database, is to use Trigger.

http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html

But this action can only be another database action, meaning you cannot have your php script be notified about it.

So in your case, its best to run a cron job every 5 minutes or whatever time interval you prefer.

Try these links:

http://www.werockyourweb.com/cron-job-mysql

var auto_refresh = setInterval(
function ()
{
    // call your php through ajax -- which inturn checks your db
    $.ajax({
      url: "script.php",
      type: "POST",
      data: {id : menuId},
      dataType: "json",
      success: function(){
        alert('success');
                    //if there is data to be populated, play sound and populate you div/textarea

      }       
    });
 }, 10000); // refresh every 10000 milliseconds

for the sound you can try this Cross-platform, cross-browser way to play sound using jQuery 1.4?, i have never tried but the answer has been accepted so should work. if it doesnot work you may also try this https://github.com/admsev/jquery-play-sound