Does IE go into sleep mode? - javascript

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.

Related

Prevent Chrome 80 from freezing tab with my page

I have a simple html page that displays some monitoring results, it reloads every 2 minutes and makes noise when those results are not acceptable. I keep this page open 24/7, so new Chrome started freezing and suspending it, as I found out via chrome://discards/ .. So the page stopped making noise and catching my attention, mostly cause the tab gets frozen by Chrome. That happens if I forget to leave the tab with this page active, "on top". Is there a rather simple way to prevent Chrome from freezing the tab, even if it is not active? I could involve some Javascript, if needed..
I am aware that there is a simple solution of opening new window with this page and leaving it alone. On the other hand, I made the page, so I can make changes to HTML in order to ensure that page remains unfrozen. I tried updating title with current time on every reload, this visibly helped, page seemingly always has fresh time in the title, but it still doesn't make noise. And if I switch into it when noise is supposed to happen, - it starts the sound half way like it was trying to all alone.. it's even funny..
Is there any simple solution to this? Thanks for your help.

Cookies resetting on page refresh but only certain user

I knocked together an advent calendar for a friend of mine and I use cookies to store whether or not she has "opened" a door - if she has, it shows a thumbnail of the contents. If she hasn't, it shows the "unopened" background. Now, I've had this working fine on my computer and in Chrome, FF and IE for over a month now.
Unfortunately, my friend isn't having the same result. When she loads the page, it shows as pristine and unopened every time. The thing is, when she clicks on it to "open" the door, it updates to show the thumbnail and if I get her to click on the padlock at the top of the address bar in Chrome, it shows that the cookies have been created and the correct one has been updated. Unfortunately as soon as the page is refreshed, it's somehow overwriting the cookie value.
Before clicking on Door 1 - cookie value is 0
After clicking on Door 1 - cookie value is 1
And yet when she refreshes the page, the cookie value is set back to 0 again.
I have NO idea what would make it do that. There IS a piece of code that checks if a specific cookie exists and if not, creates the list the cookies, but even disabling that script call doesn't stop the problem. If anyone has any ideas, I'd appreciate it because I've tried everything I can think of to make sure the cookies are being set properly and I'm not sure why they aren't persisting for her. I've asked her to check setting for storing cookies, extensions, ad blocking/script blocking stuff, antivirus, etc.
We're both using Chrome on Win 10 - it works for me, but not her.
Thanks in advance, I'm sure its something really simple and I'm just being slow-witted.
The website is here.
Started working spontaneously.

Play sound only in one window of which is active (within one website) using javascript (possibly jquery)

Alright, I've found a bunch of answers concerning native functions like window.onblur and window.onfocus... But they won't help so I'd like to be more specific
Say you open several tabs of one website
Say you receive a message and there's a sound to announce the message
As you have several tabs opened, you will hear the sound the number of opened tabs. Which makes a how'd'u'callit symphony
Best solutions I've found so far, but which don't work
1. window.onfocus and window.onblur
2. Play sound if var infocus evaluates to true, don't play if not
3. It is crossbrowser
4. It is simple
5. It does not work
Why the best solution won't work? Say you switch focus to another tab of a different website, your website loses focus so you won't hear the sound. Even worse, say you switch to another program, then the browser itsel loses focus and you won't hear the sound
So what shall I do?
You could save the timestamp of the last onFocus() event in a JavaScript variable and in a cookie (access set to your website root). Then when you want to play the alert sound, you compare the current values of the variable and the cookie and only play the sound if those two match.
Alright, two weeks after it seems like I've found the real solution. Which actually proves that if you want to do something, don't ask for help, just do it
This is what I did:
Create a cookie with a randon id and the current time (winid + t1). The cookie is created by each opened tab on loading.
document.cookie = 'winid='+winid+t1;
Create a function which will update the current time in the set cookie, say, every 3 seconds (I kindda don't like to overflow clients, so it's 3 secs not 1). If the function finds out that the winid in the cookie and the winid of the current tab don't match and 3 secs have elapsed, then the tab was closed, redefine the primary tab inside the same function.
window.setInterval(setwinid,3000);
This is it, every time you need to, say, play a sound, you should check first, whether it is the tab which is to play it
The trick is that each tab has its own winid. But only one winid is stored in cookie, is updated and thus allows the one tab to perform actions. Pretty simple. I actually started using this method for updating messages in the box across all tabs not only for playing music
One solution would be to have a server-side solution that would play the notification only once. You don't specify how the site receives the messages, but I assume it's some form of AJAX call that gets the message from the server and the messages are saved in a database.
Add a flag to the database that signifies that the message has been sent. Set the flag the first time the user's browser queries for new messages. On the page itself play the sound only if the flag has not been set, otherwise don't play the sound. Now only the first page that fetches the message will play the sound.

How to keep website's javascript timer enabled while iPhone is locked

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.

Android browser: Some javascript functions stop working randomly

I am writing an android (2.2) app which launches the default native web browser via a new Intent to a page I've created. On that page, it pauses for 2 seconds before then submitting a form, which redirects the user to another page.
I am using javascript's setTimeout() function.
It works most of the time, but then sometimes it just stops working, and won't work again until I restart the phone!
There are NO javascript errors which appear in LogCat.
Even stranger, to counteract this I put a link on the page that says "If the page has not redirected in 5 seconds click here". It submits the form when clicked, via onClick() and .submit(). Well, again, it usually works, but, when the setTimeout() function stops working, so does the link! But, I have confirmed that alert() still works, so some javascript functions apparently still work.
Any ideas why setTimeout() and .submit() would just randomly stop working, until the phone is reset?
EDIT: I've also confirmed that window.location doesn't work anymore either, but, if you just have a regular link, it does actually let you click on it, and it leaves the page.
EDIT: It appears that you can get this problem to happen more quickly if you hit STOP and REFRESH a few times while waiting for the setTimeout to fire off. Basically if I hit stop, then refresh, then stop, then back, etc, a couple times, I can get this problem to happen. After it starts, no amount of refreshing or killing/restarting the browser will make these javascript functions start working again.
Thanks!
Richard
I came across this post that may be of help: http://www.barneyb.com/barneyblog/2011/02/20/settimeout-bug-in-android-2-2/

Categories

Resources