I have php file, that generates json with a countdown seconds to the New Year eve and to some birthdays. But I want the countdown to change every one second. Is it safe (will it overload the server) if I setTimeout(1000) to my ajax call function? And what's the best way to implement 1 second JSON call via jQuery? Thank you
I have php file, that generates json with a countdown seconds to the New Year eve and to some birthdays. But I want the countdown to change every one second. Is it safe (will it overload the server) if I setTimeout(1000) to my ajax call function? And what's the best way to implement 1 second JSON call via jQuery? Thank you
Share Improve this question asked Dec 12, 2013 at 16:12 crazynamecrazyname 1351 silver badge15 bronze badges 13- 1 why would you need ajax for that? – A. Wolff Commented Dec 12, 2013 at 16:12
- 3 Whether it will overload the server is dependent on your server. What's stumping me even more is why you need to use php at all? Just do it in javascript. – Jamie Taylor Commented Dec 12, 2013 at 16:14
- 1 @A.Wolff As he mention: ...and to some birthdays He might get birthday details from db. – aksu Commented Dec 12, 2013 at 16:14
- 3 @asku Still, why do you need ajax? Load all the birthdays into JSON on page load. – Styphon Commented Dec 12, 2013 at 16:15
- 2 Network latency will cause it to lose time... – logic-unit Commented Dec 12, 2013 at 16:23
6 Answers
Reset to default 5Instead of overloading your server with requests (take into context you might have many users doing many requests at once coupled to your constants callbacks), take your data all at once for the following day for example and process the birthdays only in javascript.
The only Ajax portion i'd use in there would be to load more birthdays once per hour or day in case you have a really hyped user that leave his browser open.
The load on the server depends on:
- The amount of work the php does each time it is called and
- The number of users you expect
Depending on what the server is doing, you may well be able to move the countdown logic into the client javascript, with the server just calculating the initial values. You can then use a timer in javascript to update every second, calculating the difference between the current time and the starting values.
Depends on your server and how optimised your PHP endpoint is... its a very conditional question.
However for what your doing, I would also suggest doing it all from JS.. even with birthdays, I would set a JSON object in the markup with all the birthdays that are recorded, then you can do it all client side... maybe call ajax every 5-10 minutes for any new birthdays that may be added... depends on what your application is.
I used setInterval, and checked if the remaining time equals zero. Then I rerun the ajax function.
thank you, all :) you were helpful after all ;)
if I understand, you want to call your server every second to update your time.
Can't you just :
- Call your server to get the actual time
- List item 2 - Process with a JS
setInterval(functionIncrementAndUpdateDate, 1000);
?
You could use Keith Wood's countdown timer: http://keith-wood.name/countdown.html
It is extremely easy to use and no need for AJAX. All you have to do is include the plugin file and write:
<div id="timer"></div>
$(document).ready(function() {
$('#timer').countdown({
until: '<?php echo date("h:i:s"); ?>' // change this to New Year or a Birthday from DB
});
});
Demo: http://jsfiddle/tqyj4/436/