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.
Related
i need your help.
I need to make a real time timer in my webpage, but i need to operate it from backend (ex like start stop or reset).
Can anyone help me?, i didnt have any idea to make this thing
Sorry for my bad english.
thanks.
ps : the timer's time is same in all of users
ps : the timer's time is same in all of users
this would not be easy.
When admin start/pause/resume/stop timer. all client timer will started.
realtime
prevent bunch of request to server.
javascript only is not possible to doing this. if you are using javascript to check every certain time (setInterval), this will hard for server
to make this happens, i recommend you to use https://socket.io/
example: realtime chat, realtime notification is using websocket
good luck
You want something like this?
How to create a JQuery Clock / Timer
This takes your date/time, and after every second it updates your time on the screen.
I am currently making a timer in javascript and I want to make sure that if you close the browser that its not just lost...so as my clock is ticking away I am storing a cookie every 100ms.
It's every 100ms as I am using the system time instead of just a system interval so I am basically just calculating the difference in time between the start and now and just want to update that as nicely as possible.
I could reduce it to 500ms I guess but its still basically doing the same thing...
I'm just wondering if storing cookies this way is a bad thing in any way and if it is a bad thing is there a better way to be doing this?
Cheers!
I am not sure what the "big picture" is, but here is my take on it.
If your web page is showing a count-down to a certain event, I would split the responsibility between server side code and client side.
On page-load get data from the server on time-remaining and start the timer. No need to store anything in cookies. On page-load, you can get the data from the server again, so nothing is lost.
I want to measure the time it takes for a user to complete a task (answer a quiz). I want to measure it accurately, without the network lag. Meaning, if I measure on the server side the time between 2 requests, it won't be the real time it took the user, because the network time is factored in.
But on the other hand, if I measure in javascript and post the timestamps to the server, the user will be able to see the code, and cheat by sending false timestamps, no?
How can I get the timestamps in javascript and make sure the user doesn't fake it?
Generally in client side code, any question that starts off with "How to securely..." is answered with "Not possible". Nothing, not even putting variables in a closure (because I, the evil cheating user could just change the code on my end and send it back to you).
This is the kind of validation that should be performed server side, even with the disadvantage of network latency.
The trick here would be to measure the time using JavaScript, but also keep track of it using server-side code. That way, you can rely on the timestamps received by the client as long as you enforce a maximum difference between calculated times. I'd say a few seconds should be good enough. However, by doing so, you are creating an additional vector for failure.
Edit: A user could potentially tweak his or her time in their favor by up to the maximum enforced difference if they are able to take advantage of the (lack of) network lag.
I faced same problem while designing an online examination portal for my project.
I went for a hybrid approach.
Get time from server as user loads the page, and starts timer based on javascript. Record the start time in your database.
Let the timer run on client side for some time, say 30 seconds.
Refresh timer by making a AJAX call to server for timer reset as per the time that has passed already.
NOTE: try to use external javascript and obfuscate the code of timer to make guessing difficult.
This way you may not prevent user completely from modifying timer, but you can limit max possible error to 30s.
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/
In other words, what technology would take care of doing the time tracking? Would it be the JavaScript? I don't see being able to keep track of such things with PHP.
Here's what I need to accomplish:
I need to have a long form spanning many web page reloads because it is pretty much an online test where each page load displays a new question. However, the entire form, which constitutes one test has a time limit. When the time expires if the user in question has not completed the test then he/she cannot submit a partially completed test nor attempt to do the entire test all over again within the same day (either calendar date or with 12/24 hours in between.) The user may, however, comeback the next day and attempt to finish the entire test again within the allotted time. I know I have added a lot of details and I did this just to show context, nevertheless, the main difficulty for me would be in how accomplish the time expiry feature. That is, somehow, within a series of pages that make up a form representing an online test I want to track the time starting from the first question (one page load) and upon time expiry for the test to be disabled.
Has anyone ever done this? Would anyone have any tips for me on how to accomplish this?
Any advice I can get would totally be appreciated in advance.
If you do track time on client-side - always validate it on the server-side.
Never trust the client, by itself, to validate the time. As mentioned in the comments, client-side time validation is only good for cosmetic features, never for actual validation.
The easiest way to accomplish this is to add a unique token to the form (which is not spoofable) on first navigation. Cookies, or any other sort of session management technique you get from your framework will suffice.
On form submission you can first validate this on client side and return an error if time has passed, even before actually sending the form. If successful, submit the form, and make sure you validate the token upon processing on the server.
There are two ways you could measure the "time they spent" on the form.
When the first page of the form is severed, in the PHP create a session variable containing the date. When they finish the form, you subtract the current date form the beginning date. This gives you the total time it took from when the form was served and finished. However, this is not 100% accurate as there could downtime for other reasons such as slow internet.
You could have JavaScript record the time on that page (I answered how to do that here: here) Using AJAX, this time could be sent that way or by using GET posts. The time would then be kept by PHP somehow and added up in the end.
Hope this helps! Just ask if you want an example.
In the most general terms, you'll need to set up a session on the server side to track each user and test. When the user begins the test, you stamp a variable (server side) with the test start time. As the user progresses through the test and requests additional pages, check whether the difference between the current time and that variable have exceeded the time allowed for the test. If the test has expired, instead of delivering the next test page, you can close up the test and deliver a "time's up" page to the user.
I don't know what server-side environment you're using, but it almost certainly has some sort of session management framework available. To reliably control the testing environment, you have to manage this from the server side.