开发者

Timer with Ajax and mysql

开发者 https://www.devze.com 2023-02-13 08:07 出处:网络
I\'m pretty new to ajax, php, and mysql. I think I am doing something very un-scalable and was hoping for a point in the right direction.

I'm pretty new to ajax, php, and mysql. I think I am doing something very un-scalable and was hoping for a point in the right direction.

Right now I have a timer on my site that works by using ajax to get a page which contains a time. The page that has the time, when requested, queries my mysql data base for a "target" time and compares the difference between the "target time" and the current time (using time()). Ajax is set to an interval of 1000milliseconds. Sometimes is skips seconds for the user.

So right now, for every user, every second my mysql server is queried. Is there a way that only the page being retreived by ajax queries the mysql server every second and then the value is saved and the users just query the value on that page... I hope this makes sense.

What I want to do: have a page query my mysql server every second to get a time, and have this page served to users on request. = 1(mysql query) x 1000(pages retrieved by ajax) the work (given 1000 users)

What I have now: a page retreived by aj开发者_如何学运维ax that queries my mysql server every second. = 1000 (mysql queries)x1000(pages retreived by ajax) the work (given 1000 users).

And also, whats the proper interval to set it to? 500 milliseconds?


When the page first loads, you could set a javascript variable, whose data is set by PHP time("U");, which is the unix timestamp of now for the server.

Then when the page renders, subtract the clients unix time:

var foo = new Date; // Generic JS date object
var unixtime_ms = foo.getTime(); // Returns milliseconds since the epoch
var unixtime = parseInt(unixtime_ms / 1000);

giving you an int, which you call delta. Now using javascript alone (no ajax), you can set a 1 sec timer to update the time, basing calculation with delta.

0

精彩评论

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

关注公众号