Is it possible to run a timer in the background (in both iOS and Android) or will it be out of the app-guidelines (specifially on iOS). Right now I am having a meditation-timer-app and one of the most important things is that the time should keep on running while in background mode (if the phone display is black or the home screen is opened).
I am using an interval setInterval() - Method of 1000s to count down each second.
https://capacitorjs.com/docs/v2/apis/background-task#background-tasks says it may not be possible:
NOTE: On iOS setTimeout and setInterval won’t work once your app is in background, so don’t use them inside beforeExit.
So another idea that I would have would be to make a calendar event (for example for a Date that is 10 min in the future if you set the timer to 10mins).
Than it would be a scheduled timer event. If you reopen the app, the timer gets adapted to the calendar event.
Is this possible to wait for a specific date to happen? And how?
Do you have a better solution or can I just use the interval in the background somehow?
For my app this would be very important, since when meditating, you don't want to have the phone open. You want to start the timer and put the phone away.
Related
I've looked all over the net for a solution to the following problem: how do I create a timer that works in an inactive tab on any modern web browser?
Solutions appear to exist using JavaScript (using setInterval / setTimeout and using a Date object / timestamp), but these require the user to refocus the window to reload the timer. What if the timer finishes and the window is out of focus? I need the timer to make a noise or alert the user in some way, so these solutions do not apply.
I'm asking for a solution using any server-side or client-side language.
setInterval and setTimeout should work even if tab is not in focus (as any other actions).
Try to write this in console of new tab and switch to another tab:
setTimeout(function(){alert('hello')}, 5000)
After 5 seconds some kind of mark would appear on tab.
This is how it looks in firefox:
I've got a simple timer that I built that works off of a setInterval function. Every 1 second, it increases the value of the timer by 1. When I click off of the tab to a different tab in Safari, it slows down. You can see what I'm talking about in this basic CodePen:
http://codepen.io/anon/pen/rxVMRp
This is the basic JS code:
function setTimer(){
var curTime = parseInt($('.timer').text());
var newTime = curTime+1;
$('.timer').text(newTime);
}
setInterval(setTimer, 1000);
Granted, my timer is a bit more advanced, but you can still see the slow down effect of setInterval in Safari in this basic example. How can I prevent this slow down without having to offload anything to a web worker?
Is there any particular reason you're using a setInterval here? Probably the reason Safari is slowing down on blur is to prevent tabs from wasting CPU cycles when the user isn't paying attention to them. What could possibly be so important in your code that it needs to be updated every second, even when the user isn't focused on it?
At any rate, you could listen for onblur and onfocus to pause or restart the timer as necessary. If you need to keep perfect timekeeping for some other purpose there is always Date.now().
I have this functionality built in Jquery/ javascript where a user is notified when their session is about to be timed out. Then they are given an option to extend it. If they don't make a choice the pop up closes itself and the browser is redirected to the login page. It worked perfectly fine for a while. But now I noticed it works correctly only if I am active on the computer. If the computer is left unattended for an entire day, the pop up does not begin the countdown until the user unlocks the computer and logs in again.
Is anybody aware of this behavior where ie stops executing javascript when the computer is left unattended for a long time?
Update: Is there a way to keep the tab from sleeping? Without that, the browser won't be able to redirect at the right time.
setTimeout only works when the tab is active. In some browsers even changing tab will make it stop counting. So not only if you are on the computer but if you're not on the specific page it might not work. Also on mobile devices with multitasking it's bound to fail, forget about tabs, applications often go to suspended mode.
Take a look at this question, it offers the same solution as Luka with a code example:
How can I make setInterval also work when a tab is inactive in Chrome?
You might want to do two checks on the time passed, one to check if you need to show the popup, and one to close the popup, using the total time passed instead of having a different count down.
i would suggest logging the current time when the page is loaded and then calling a function every 10 seconds of so that checks if the time passed is more than x amount, the reason your problem occurs is most likely because the default timeout function only counts down while the page is being rendered.
In my Web app, the user sees a countdown timer (that each second updates the number of minutes and seconds remaining) and can press "+1" and "-1" buttons to increment a total value. (The use case is counting the number of people who the user can see in the given number of minutes.)
I'm using setInterval but it's running into problems with the fact that there's only one execution thread: when the user presses the +1 and -1 buttons, it interrupts the timer and throws the display off.
On the desktop, perhaps a separate Flash movie for the timer display is a solution. Our users also use the Web app on iPads, so Flash isn't an option there. Any other suggestions? Could a PhoneGap wrapper allow for multiple threads?
Or what about WebWorkers? I guess the problem there is that the worker thread couldn't access the DOM to edit the timer display.
When my website is opened in Safari and the iPhone is put into sleep mode, any javascript timers (window.setTimeout) are not executed any more.
Is there a way that these timers get executed even if the screen is locked? It would be sufficient if this happens only if Safari is the active app and the website is the active "tab" in the moment of locking.
I read that apps can prevent "deep sleep" mode by regularly playing a soud. Does this also apply to websites?
Simply change your logic so it doesn't require constant ticks. If it's a countdown timer you could simple store the date every tick and compare the previous date to the current one in each tick - even if you got no ticks for a long time you exactly know which time passed since the last tick.