Is it safe to call jQuery ajax every 1 second - javascript

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

Instead 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.net/tqyj4/436/

Related

SetTimeout or use database to track next function trigger?

I have an app where I'm updating data from various APIs and storing it locally. The data in each API is updated at different intervals...3 seconds, 15 seconds, and 30+ seconds.
At the moment, I'm updating each API and then setting a setTimeout to schedule the next update. It works...but is this optimal?
Another option I've considered is to include a field named nextUpdate in my database model that takes a Number (Unix timestamp), and then query the database once per second for any objects that are scheduled to update with mongoose, such as .find({ nextUpdate: { $gt: Date.now() / 1000 }). My concern was that this would cause too many unnecessary calls (and frankly this is my first app so I don't know how many mongo requests per second is considered too much). I'm currently using Mlab as my database host.
So would you continue using setTimeout? The database refresh option I've proposed above? Or another solution?
Thanks in advance for your time and advice.
I would keep using the first approach, though setInterval would be more fitting here.
The second approach seems like it only has downsides to it, unless I'm missing something?
(I would have liked to post this as a comment but cannot post comments yet)

set javascript time to a specific timestamp

I need to get a uniform javascript time for all clients in our private server. So I want to set the javascript time of a client on initialization to the time of the server.
Right now I am thinking of setting a setInterval that will increment a variable that has a timestamp, but I think it would be too much to have a setInterval running in the back every n milisecond.
So is there a way for me to set the starting time of javascript? so every instance of new Date will be based on that, not the system time of the client?
A difference of 1-2 seconds from the server time is ok but if a difference of milliseconds is achievable then that would be better.
Any suggestions?
Have your server output it's current timestamp, and then calculate the difference between the client timestamp and server timestamp.
<script>
var serverEpoch = 1408602887; // written dynamically by the server
var epochDiff = Math.round(Date.now()/1000)-serverEpoch;
</script>
Now you have the difference between the server time and client time in seconds stored in epochDiff, which you can use for time calculations.
As I mentioned in my comment, this only works if your pages are generated constantly generated rather quickly. If the time can fluctuate (say between 5-2000ms), it would be a better idea getting the server time dynamically from a dedicated, fast script using XMLHttpRequest().

Detecting changes in database after regular intervals of time using php and mysql

I am trying to ping my database in a regular intervals of time so I can check the status of a column. Like in every 3 seconds I want to know the status of a column.
You can write a php script to retrieve that value and run that script at regular intervals i.e. 3 seconds in your case .
something like this for local
*/3 * * * * /usr/bin/php -f /var/www/cron.php
If you need to call your db every 3 seconds you can use a javascript something like this setInterval(function(){},3000); to call your analysing function , but this isnt a good practice.
If your purpose is to know the time and context of ur field value being changed you have lot of other ways like using triggers is the best solution for this purpose, Check this link to know more about triggers
https://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html
also go through
Are database triggers evil?
hope this will help you.

timer plugin for a game

I am looking for a jQuery plugin that can display a timer with miliseconds and also return that time to save a highscore. does anyone know a good one? It should also at least try to avoid cheating.
Thanks
I think that to make a tamper-proof timer, it is not enough to use a client-side JS plugin, you will have to check the time on the server. For example, send an AJAX request on start and on finish and measure the time between them.
As for the clock plugin, you can for example use epiclock, but do not rely on it to measure the time.

Live countdown clock with django and sql?

If I make a live countdown clock like ebay, how do I do this with django and sql? I'm assuming running a function in django or in sql over and over every second to check the time would be horribly inefficient.
Is this even a plausible strategy?
Or is this the way they do it:
When a page loads, it takes the end datetime from the server and runs a javascript countdown clock against it on the user machine?
If so, how do you do the countdown clock with javascript? And how would I be able to delete/move data once the time limit is over without a user page load? Or is it absolutely necessary for the user to load the page to check the time limit to create an efficient countdown clock?
I don't think this question has anything to do with SQL, really--except that you might retrieve an expiration time from SQL. What you really care about is just how to display the timeout real-time in the browser, right?
Obviously the easiest way is just to send a "seconds remaining" counter to the page, either on the initial load, or as part of an AJAX request, then use Javascript to display the timer, and update it every second with the current value. I would opt for using a "seconds remaining" counter rather than an "end datetime", because you can't trust a browser's clock to be set correctly--but you probably can trust it to count down seconds correctly.
If you don't trust Javascript, or the client's clock, to be accurate, you could periodically re-send the current "seconds remaining" value to the browser via AJAX. I wouldn't do this every second, maybe every 15 or 60 seconds at most.
As for deleting/moving data when the clock expires, you'll need to do all of that in Javascript.
I'm not 100% sure I answered all of your questions, but your questions seem a bit scattered anyway. If you need more clarification on the theory of operation, please ask.
I have also encountered the same problem a while ago.
First of all your problem is not related neither django nor sql. It is a general concept and it is not very easy to implement because of overhead in server.
One solution come into my mind is keeping start time of the process in the database.
When someone request you to see remaingn time, read it from database, subtract the current time and server that time and in your browser initialize your javascript function with that value and countdown like 15 sec. After that do the same operation with AJAX without waiting user's request.
However, there would be other implementations depending your application. If you explain your application in detail there could be other solutions.
For example, if you implement a questionnaire with limited time, then for every answer submit, you should pass the calculated javascript value for that second.

Categories