AJAX how to make real time timer to all user - javascript

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.

Related

Is it bad to make a cookie every second or less?

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.

Executing codes on website without the website being accessed PHP

I don't know if this is possible or even if it exist, but I'm very curious to find out. I don't want to give the wrong ideas, and describing what I'm trying to achieve before giving out examples definitely will - so I'll just dive right into it.
As far as I know, codes on websites are only ever executed when those websites are accessed by someone. If no one accesses those website, the codes just sit there. The codes have no reason to run if no one's using them, right?
Now what I'm going to propose may sound ridiculous, but please hear me out. I don't know if there is a way to do this, so I'm just going to ask. Is there a way to run those codes without someone accessing the website itself?
Now I know some of you are like, "Huh? What is he talking about? Why would you even want to run the codes if no one is on the website? That literally makes no sense," so I'm going to try and justify why I want something like that to be possible.
For example, if you want to create a script for automatically logging out an user if they've stayed inactive for a certain amount of time, you would need to check whether they've been active in the last (amount of time to wait before logging them out). You can use AJAX to check if they've been active in the last (amount of time to wait before logging them out). If they navigates, or refreshes the page, then it'll reset the counter, and let you know that they've been active in the last (amount of time to wait before logging them out). However, if they do nothing in (amount of time to wait before logging them out), they will be automatically logged out.
If they closed their browser, or exit the tabs that monitors their progress using AJAX, it will no longer monitor their progress, and thus their counter will not be updated, and thus you will have no idea whether they've been active or not. You can't just log them out if they close a tab or a browser, because what if they have multiple tabs or browsers of your website open? Then you would only want to log them out when they closed all of them.
I have other examples, but this is the gist of it. Is there a way to execute codes on a website without the website being accessed by a user? Thank you.
You are looking for cron jobs. They are basically scheduled jobs that run at set times. A cron job can run all kinds of scripts including PHP scripts.
Whether such a script can easily clear expired sessions, I don't know. It will probably depend on the way you store the sessions.
It may be just as easy to implement it in the website. If you store the last activity timestamp of a user, you can just check on a new request whether that timestamp is too old, and if so, delete the session and redirect to the login page. That way, the user officially remains logged in until their next request.
Optionally you may delete old sessions that are remembered by PHP. See related question: Cleanup PHP session files.
One approach is that you could run your PHP scripts on a timer using CRON jobs.
These jobs typically repeat every x hours, minutes, or days.
I'm not sure about the example you provided, though.

Time check function in extjs?

I'm trying to make an email form with extjs 4 that sends an email to 12000 company members at a time.
The server wouldnt hold that large number at one time, so im trying to write a function that generates the sending method every three messages or something.
Does anybody know of a suitable function? Sample code? Or a way to refresh an extjs form every few seconds/minutes/etc?
Not sure that's the best approach. But for refreshing a page it's basic javascript
document.location.reload(true)
and use a setTimeout to repeat the reload.
So a reload afther 5 seconds:
var t = setTimeout(function(){document.location.reload(true)}, 5000);
But I rather advice using an ajax call and a setInterval to repeat every x ms so you wouldn't have to reload any page at all!
An even cleaner approach is to handle it on the backend.
Another helpful article
Another helpful article

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