Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript setTimeout not firing after Window.Open

I have an ASP.Net web page that currently works on the iOS4 but not on the iOS5.

On the parent web page I have a button that opens a child window. And I do a setTimeout refresh thing in the parent window right after the open window call.

I noticed on the iPhone iOS5 that when it opens the child window, the setTimeout function in the parent page is not called until I go back to the parent window and then back to the child window to see the update.

Here's a snippet of my code in the parent page and where I think is the problem

WindowManager.OpenWindow('...')
t = setTimeout(function() { handles[0].testfx(); }, 1000);

this runs on iOS4 but not on iOS5.

Any ideas?

like image 627
Rod Avatar asked Jun 27 '12 16:06

Rod


People also ask

Why is setTimeout not working?

If you are requiring the setTimeout functions to execute on the dot, there can be some scenarios when this is not the case. Late timeouts or timeouts that execute inaccurately could be due the following issues: browser or OS is busy with other tasks.

Is Javascript setTimeout blocking?

Explanation: setTimeout() is non-blocking which means it will run when the statements outside of it have executed and then after one second it will execute. All other statements that are not part of setTimeout() are blocking which means no other statement will execute before the current statement finishes.

How setTimeout works asynchronously under the hood?

every function that is asynchronous like Ajax, setTimeout, event handlers, etc.., joins the Events table and waits for the time for execution to come for example setTimeout waits 3000 ms to run or event handler waits until Click event happens and then run.

Is setTimeout deprecated in Javascript?

We all know that passing a string to setTimeout (or setInterval ) is evil, because it is run in the global scope, has performance issues, is potentially insecure if you're injecting any parameters, etc. So doing this is definitely deprecated: setTimeout('doSomething(someVar)', 10000);


2 Answers

iOS5 does indeed pause JavaScript when a window is not the active window. There is nothing you can do about this, so the best you can do is try to alter your setup so that JavaScript does not need to be run when your window is in the background.

You said you are doing a setTimeout refresh thing in the parent window. WHY are you doing this? Frankly, it sounds a little bit odd -- why do you want to be refreshing anything when the user can't even see your page? What exactly does the line handles[0].testfx(); do for you?

like image 138
Michael Frederick Avatar answered Oct 18 '22 19:10

Michael Frederick


This is just a suggestion, since I don't have access to test this, but you may be able to hack around this using by using window.postMessage. An example of this can be found here.

In your case, you would probably want to check if a required amount of time had elapsed, and if not, call window.postMessage again, otherwise, call your handler.

I'm not sure how quickly calls to window.postMessage will be processed, if at all, when not in the active window.

like image 30
Monroe Thomas Avatar answered Oct 18 '22 19:10

Monroe Thomas