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

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.

Related

Reset bootstrap-session-timeout without page refresh

We are using the Orangehill fork of bootstrap-session-timeout and it works nicely, with one exception, and I cannot figure out how to reslve this. Wondered if someone could help turn the light on for me.
Site is written in PHP with Bootstrap3 and JQuery3.
The footer of every page contains the script that activates bootstrap-session-timeout, but the option to reset the timer each time the mouse is moved is not used. If there is no activity within the timeout period, the ribbon pops up and the user is given the option of Staying Connected or Signing Out.
This is working fine for the site, however what we have is an order edit page, where users can add, delete and update lines that are on an order. The page is not refreshed during the edit. To add, delete or update order lines, the links call JS functions, which in turn call PHP scripts to perform the updates, JS then being used to update the screen contents dynamically.
We need to reset the session timeout counter each time the user updates the order.
Ideally, we would like to call a function whenever an update to the page contents is made, but none of the individual bootstrap-session-timeout functions seem to be available within our JS.
Hope this makes sense and thanks in advance for any help/pointers.
Since nobody responded to this question, I thought it pertinent to add my solution, which might not be the best, but it does work.
Basically, we have an order entry page which stays in place while an order is being entered. The countdown timer keeps going even though the user might be doing something on the page (we have the on-key options turned off). So, even though the user might have last updated the order 5 minutes ago (the updates being handled via Ajax calls to PHP API's), the countdown timer is not reset because the page has not been reloaded.
My solution is to write the timestamp to a file which is keyed by PHP session-id, every time a PHP script is loaded. This applies to the main page or to API's. So, when the countdown timer ends, a timeout script is called, and this checks whether the last activity timestamp in the file is before or after the timeout cutoff point. If it is, the logout script is called, but if not, the current page is refreshed, which causes the countdown timer to be reloaded.

How to set timer in controller?

I am a new in angular js and in this moment I am developing a simple aplication to organize tasks that employees doing during the day.
First of all, the user set the customer, description and start the timer to count, when the task is finished, the employees click in finish and the task is closed, but I make a new function if the user close accidentally the tab, when reopen the tab, the user can get the old data (localstorage), but I dont know how to set the time when the user click to active the old time.
I am using this timer (http://siddii.github.io/angular-timer/examples.html#) to develop the counter in this app.
Anyone help me solve it?
According to the documentation, you can initialize the timer by setting the start-time attribute as the time in milliseconds. You can get this from any Date object with the getTime() function. What you need to do now is take this value, store it in localStorage, retrieve it later, and use it a the start-time attribute.

Countdown Timer which resets every 12 hour

I'm trying to make a custom countdown timer that resets itself every 12 hours and won't reset on every refresh or by closing the window.
EXAMPLE - Like I've a shopping site and want to show COUNTDOWN Timer there which shows in this format (hh:mm:ss) with unlimited loop ... and it must not reset by closing the browser or window.
I think the ideal way to do this is via timezone?
Any kind of solutions are welcomed...
I need exactly like this type of countdown timer - http://www.bedeals.com/
You may persist the timer state in localStorage . Simply add the time of start there and every time you get into the page read the value and apply some functions to get current time ( based on obtained value ). The apply setInterval to update the timer on the page dynamically.
Date formatting is described here.

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

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);

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