PhoneGap 2.7 Timing Issue

75 views
Skip to first unread message

anev...@gmail.com

unread,
Jun 26, 2013, 4:46:22 PM6/26/13
to strati...@googlegroups.com
Hello,
 
We have been using oni-apollo.js with PhoneGap 2.5 successfully.  We are upgrading to PhoneGap 2.7 and there is a problem.  It seems that the PhoneGap onDeviceReady function is called before oni-apollo is finished working its magic with the page's inline javascript.  Then when we try to call our startup function we get this error:
 
ReferenceError: Can't find variable: myStartupFunction
 
This happens about 75% of the time when we launch our app, and about 25% of the time it works just fine.  We introduced a setTimeout wrapper around calling our startup function with a delay of just 100ms, everything works 100% of the time.
 
Okay, so a delay of 100ms is not a great solution because we have no idea if that threshold is going to be long enough on all devices.  So my question is, how could I register an event or be notified that SJS is finished working its magic with my inline javascript and that I can now safely call my functions?

Tim Cuthbertson

unread,
Jun 27, 2013, 4:34:14 AM6/27/13
to strati...@googlegroups.com, anev...@gmail.com
I haven't used PhoneGap much, but I looked into this and it seems that (at least on the latest phonegap, I have 2.9), deviceready is a sticky event - even if you don't register a callback until after it's happened, it'll fire the callback. So I assume you've currently got something like:

                document.addEventListener('deviceready', run, false);

in a plain javascript section. If so, you should be able to just move it into the bottom of your text/sjs block, and it should work. If SJS is executed before the device is ready then you've registered the handler before the event happens, just as JS would. If SJS loads after the device is ready, phonegap still seems to trigger the event.

If that doesn't work (perhaps this is new to 2.9), note that inline SJS and JS in a html file share the same global scope. So you can use a shared variable for this purpose:

        <script type="text/javascript">
            var deviceready = false;
            document.addEventListener('deviceready', function() {
                deviceready = true;
            }, false);
        </script>
        <script type="text/sjs">
            function run() {
                console.log("let's go!");
            }

            if (deviceready) {
                run();
            } else {
                document.addEventListener('deviceready', run, false);
            }
        </script>

.. but hopefully that's not necessary.

Hope that helps,
 - Tim.

anev...@gmail.com

unread,
Jun 27, 2013, 7:56:54 AM6/27/13
to strati...@googlegroups.com, anev...@gmail.com
That did the trick!  You've got to be exactly right, that the deviceready event gets fired even if I don't register the callback until after it has happened.  And moving my addEventListener line down to the end of the SJS block worked like a charm.  No need for a 100ms delay anymore.  Thanks a ton!

Tim Cuthbertson

unread,
Jun 27, 2013, 9:02:20 AM6/27/13
to strati...@googlegroups.com
No worries, glad it worked :)


--
You received this message because you are subscribed to the Google Groups "StratifiedJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stratifiedjs...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages