What is an ideal time to update modified content field of table and show it to Users? - javascript

I am working in an application where I have to show application users that contents of this page was modified like :
1 min ago
5 min ago
2 days ago
something...
Then what should be ideal time interval to fire an ajax call to get last modified date for contents from db and to show to users?
JavaScript has setInterval() where we can set function (to fire ajax call and get last modified date) and time interval (based on which a function will be called to fire ajax call).
Why I am asking because firing ajax call within very few seconds can be considered as an overhead to application.
Please suggest me on this.
Thanks in advance.
Edit:
I am appreciating your comment and checked plugin. But whenever any changes are made to DB table there is one parent table which has modifiedDate field which is updated on every changes and that I have to show to users.

You don't want to waste calls to a DB to get an interval. No single interval would be correct and it would be just pointless updates.
Just use a simple JavaScript plugin like timeago.
It makes it easy by just printing the original date/time and the plugin does the rest auto updating your user as they view your page, so this date:
<abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr>
Turns into this for the user:
<abbr class="timeago" title="July 17, 2008">5 years ago</abbr>

No need to send ajax requests repeatedly. Just get the date / time with the main contents, and place it in a hidden field in HTML. Then using javascript/jquery, get that date / time and process it, and display it on the appropriate place(s).

unless you are familiar with websockets i would make the call every minute OR just refresh the page every minute so you can fetch the last modified date accurately.
setTimeout(function(){ location.reload() }, 60 * 1000);

Related

Run Function in Laravel after a certain time and show this time as a countdown

So I have a function and I want this function to run after(like 10 min) user selects it. While timer(10 min) is counting down to run this function, I want timer to be shown to user in a spesific view. I have all functions and views set up for this aim just I dont know how to put timer and show it to user.
https://www.w3schools.com/howto/howto_js_countdown.asp
I checked this but I cant properly put PHP values into Javascript so I cant make it. I also thought about cron jobs but I am not sure how I can do it there.
1) I want a function to run after a spesific time.
2) Additionally I would really like to show this time as a countdown to user.
If you want function that will run after certain time at serverside - check laravel.com/docs/5.5/scheduling. It's really easy to setup.
You can create Laravel's with your function that will repeat every ten minutes or whatever you want. That job would get date from database and check if function should be executed - if so, mark database record with date as executed and call your function.
For timer, in my opinion best approach is to make AJAX call to some API which would return your date for timer, then all need to be done is assign it to a javascript's variable.
Another way is to render view template also with date, make it invisible with CSS display: none and then retrieve it from DOM for your JS timer.
You would need make some changes to timer at w3schools. Add some conditionals to check how much time left, and if it's =< 10 minutes, append timer to the DOM.

How can I make a live thread (vidiprinter) type section of my site?

I need to make a section of my site like a vidiprinter stlye thing where I can update it and the page will refresh automatically on the user's device without them having to refresh. Ideally if I could allow other people who I specify to update the thread as well that would be great but is not essential. Is there any way that I can do this? (As simple as possible)
Thanks!
Have javascript check / make an ajax call to some back end page to check for updated. I'd also save the time stamp of the last time the page was updated, and then check if the newest comment / update time stamp is greater than the old time stamp, then update the page either by refreshing the whole page, or by adding the info right there from an an ajax call.

How to prevent countdown timer from resetting when the page refresh

Hello Guys I'm having a problem. The countdown of my Page resets when the user refreshes the page. Is there a way to prevent it from resetting? like whoever goes to that specific page the timer will be how it used be on how I left it? not whoever goes to that page but for everyone who goes to that specific page just like the countdown timer in this site
playrps.net/
here is the code:
http://myanimesekai.com/countdown.js
Example:
http://myanimesekai.com/time.html
I've found a lot of question that is similar to this but no one have answered them yet..
You'll have to save the start or end time for that user.
You can do that server-side if you are using php or the like but saving it to a session.
You can also do it client-side by saving it to a cookie or to localStorage.
It depends on what your counting down towards:
The page you mentioned uses an absolute time as the reference to calculate "count down":
var someTargetDate = '07/09/2014 12:00:00';
So every time someone loads the page, the counter value is calculated by something like:
var counterValue = Date.parse(someTargetDate) - Date.now();
However, if the target date is not known statically and can't be hardcoded in your HTML, for example, if you want to set the "targetDate" to be exactly a day from the first time a user goes to your webpage.
In this case, you need to set (and keep track of) targetDate in the users' sessions via either:
A backend server API which returns the targetDate OR
Browser-based stored (e.g. cookies)

Counting time in JavaScript and precision of the results

I decided to use this countdown script, however I noticed that something is wrong.
The site is down right now, but there is Google Cache for it.
Code can be find here.
It looks like code is OK, every 1000 milliseconds instructions takes care of changing the output.
But:
I opened page with the script and left it for some hours. After this time I could see "2 hours, 2 minutes". I refreshed the page (it takes 2-3 seconds) and I saw "1 hours, 57 minutes" - is it possible that script is late?
The more time I wait before refresh, the bigger was the diffrence. What can be done to prevent such behaviour of script? What can be done to make it precise?
One option would be to have the script make an ajax request every X number of milliseconds. The request would be made to a PHP script that simply responds with the time. The Javascript would then update itself. In other words, you're using the server's current time to adjust the counting of the Javascript.

Making jQuery code static for different pages

I am trying to use the jQuery Countown plugin to let my users now how much time they have left for a certain task.
So I use the following code to start the countdown with 30 minutes.
<script type="text/javascript">
$(function () {
$('#defaultCountdown').countdown({until: '+30m'});
});
</script>
The problem is that the users are going to be navigating to different pages for a given task, and if I just include this code on all the pages the timer would start from 30 minutes every time a user goes to a new page or refreshes the current one. I am new to jQuery so I would like to know if there is a way to make this code static so that is starts after a given event(in one page) and is and stops when an event on another page has occurred?
EDIT: I should mention that the countdown will only be used for a usability test in a controlled environment so that the application will only be run on one computer.
Client side solution:
Instead of counting up to a relative time, count to an absolute time and store it in a cookie.
Each page can lookup the value and start the timer.
This is not possible unless some synchronisation mechanism is provided:
Each page passes to the other the time remaining in the query string. Initialisation code needs to be modified in order to start the countdown from the received parameter.
Each page posts the time remaining in the server. This is more difficult to implement as it requires a persistense layer in the server.
What usually happens is to have the countdown in one tab/window and open the other pages in new ones. You can ask the user not to close the original window.
I see three solutions:
1) Give starting time from server side.
2) Load other pages with AJAX, so page will not refresh
3) Use frames (worst choice).

Categories