views:

193

answers:

1

In mobile safari on iPhone or iPod Touch if a user clicks on a mailto link and then returns to the page (either send or cancel), timers no longer function inside of javascript. I've posted a bug to apple, and on openradar.

However, I was wondering if anyone out there has come across this before and come up with some sort of workaround.

update: Here is some sample code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
        <title>Testing</title>
    </head>
    <body>
        <h1 id="normal">Normal: 0</h1>
        <h1 id="timed">Timed: 0</h1>
        <h1 id="interval">Interval: 0</h1>
        <a id="clicker">Click Me</a><br />
        <a href="mailto:">Mail To</a>

        <script type="text/javascript">
            window.addEventListener('load', function ()
            {

                var count = 0;
                var interval = 0;
                var id;

                document.getElementById('clicker').addEventListener('click', function () {
                    interval = 0;
                    count++;
                    document.getElementById('normal').innerHTML = 'Normal: ' + count;
                    setTimeout(function () {document.getElementById('timed').innerHTML = 'Timed: ' + count; }, 100);
                    id = setInterval(function ()
                    {
                        interval++;
                        if(interval > 5)
                        {
                            clearInterval(id);
                            return;
                        }

                        document.getElementById('interval').innerHTML = 'Interval: ' + interval;
                    }, 200);
                }, false);

            }, false);
        </script>
    </body>
</html>

To clear up some apparent confusion about what I'm saying is a bug, its not that when you leave the page and return any timers that had been running have stopped, this is to be expected. The problem is that once a user returns to the page if you start new timers, they will never fire.

+1  A: 

That' not a bug because the browser windows is actually closed when the Mail application launches. When the user returns, it's restored but any scripts that were running will have halted.

Though this is not very nice, it's expected.

Techpriester
I'm not saying that timers that were running stop running, I would fully understand that. I'm saying that once you click the mailto link, and return to the page. I can no longer set a timer. Look at the example code, if you run that on the iPod touch or iPhone, you click the mailto link and then come back no matter how many times you click the "Click Me" link to the timers will never be reset.
Boushley
Right, but is there a page reload in between being taken off of the Safari app and going back in to touch the button? If not, it won't work. Safari caches the page and stops all page processes. You're not seeing a live page. You're seeing an image of the last cached render of the page. That's why being out of the app for too long clears this render and forces a reload. The only way to restart is to do a page reload. You could probably find a way to force this behavior. Once you get that, just set a cookie and have it start where it was left off.
dclowd9901
By the way, I had this same issue with a JS plugin I wrote recently. It was pretty infuriating, but the only workaround I could devise was firing a `window.location`.
dclowd9901
The above code shows that js still runs. When you return to the page if you click the link, the "Normal:" value still increments. So parts of javascript is still working fine. However timers are not.
Boushley
Also of note, if you reload the page i.e. window.location.reload(); The issue with timers not functioning persists. Even if you redirect the user to another page and from that page redirect the user back immediately (a hacky attempt at a fix I know...) still does not work. The page is still in a buggy state.The only thing I've found that allows mobile safari to recover is clicking in the url bar and then clicking go.
Boushley