开发者

Ajax alert when a row has been added to a mysql database

开发者 https://www.devze.com 2023-01-05 07:38 出处:网络
I am trying to create a live orders page for my website. I need the page to auto-update to show that a new开发者_运维知识库 order has been placed and to display an alert/sound whenever a new order row

I am trying to create a live orders page for my website. I need the page to auto-update to show that a new开发者_运维知识库 order has been placed and to display an alert/sound whenever a new order row is in the database.

Any ideas on how i can easily achieve this?

Thanks, -AJay


You will need to use something like Comet to be able to push data to the client.

Then, use a MySQL trigger to somehow raise an event in your server application that's holding the Comet connection open to push the appropriate data.

The less elegant way that many developers use (at least until WebSockets become popular) is to poll with AJAX for changes, but this has a high bandwidth overhead and a longer latency.


From AJAX view you should use timers in javascript like this...

// First parameter is an expression and second is a interval as miliseconds.
setTimeout ( "UpdateFunction()", 2000 );

Also i recommended to you use this code...

setTimeout ( "UpdateFunction()", 5000 );

function UpdateFunction( )
{
  // (do something here)
  setTimeout ( "UpdateFunction()", 5000 );
}

your UpdateFunction() should call a php or asp page which renew list of orders.


I would think a polling approach would do you well, as server push has many negative implications for the browser.

If going with a polling-route, I would suggest having a timed event occur on your page that will call a web method. The web method would then return data (something small like an ID) about queued orders. Compare the list of IDs to what's currently fleshed out on the page, and assuming you have something in the newly given list that doesn't exist (or vice versa), call a separate method to retrieve the additional details to display display new orders from or delete old entries.

This way, you do not need to keep a steady stream to the server (which can block the user's browser from making additional content requests).

I hope that helped at all.

0

精彩评论

暂无评论...
验证码 换一张
取 消