Window 8 JavaScript app oncheckpoint not working - javascript

I am porting a html5 game to win8. To save the game state, I call a function save_game (which uses localStorage to store some data) in window.unload, which of course does not work here. So I use WinJS.Application.oncheckpoint instead. Strangely, if I launch a game and press alt-f4, the game state is not saved. Debugging in VS with console.log in the event handler, I found that it seems to be triggered only when I resume the app. Bizarrely, if I put a break point in the code, the event handler will then be correctly executed during suspension.
Anyone has any idea why this happen? Is this a bug in win8?
Thanks in advance.

You are seeing known behavior when debugging your app with VS. When you close an app via user action (Alt-F4 / top swipe), the app is actually held open for a while by the debugger. If you set a breakpoint in oncheckpoint, debug app, press Alt-F4 and then WAIT for ~5 seconds or so, you will hit the breakpoint. The best way to simulate a user "close" event is to use the Suspend and Shutdown option from within VS that will immediately fire oncheckpoint.

Related

How can I debug calls to window.open?

I have some React app here that has a malfunction that causes the page to open a new tab with itself. Recursive. and that is rather annoying as the number of tabs runs quickly into an out of memory situation. I want to debug the code to see the stack when the window.open call happens. I do not know where in the application the call happens and so wonder if there is a way to trigger Chrome to jump into script debug mode when something wants to open a window/tab?
So you can use the debugger of chrome, and then add some breakpoint to exactly decide when the code should stop, and then you use the control to jump to the next execution and decide when to go a step back and forth.
it's available for free, all you need to do is to inspect your React app and then visit the Sources tab, there you will see the code in javascript and you can start adding breakpoint and so.
You can also add mouse event listener , like click , dbclick...
You can also trigger and debug how a specific function si running.

Cordova `pause` event on iOS

Is there any Cordova/DOM event that I can hook into on iOS when the user directly closes the app (by double-tapping the Home button and then swiping the app away)? The pause event does successfully fire when the Home button is pressed once and the app is sent to the background, but the double-tap+close seems not to do this -- at least in the emulator.
I am using the pause event to capture and store app state, so closing without saving will leave the user with no previous state to return to, or worse, an old state.
I am aware of the "iOS Quirks" warning in the documentation that says:
In the pause handler, any calls to the Cordova API or to native plugins that go through Objective-C do not work, along with any interactive calls, such as alerts or console.log(). They are only processed when the app resumes, on the next run loop.
...but unless someone corrects me, I don't think this is the issue here
This question has been asked a lot on ionic forum and the consensus at the moment appears to be that this is not possible. I had this same desire for a time keeping app. I ended up deciding to use setInterval at a frequency acceptable (for me 3 seconds was fine). Agree this should be a feature

How to trap foreground and background events in a Javascript Win8 Metro App

Win8 Metro provides callback for when the process enters the activated and suspended states but what about when the app comes into the foreground or goes into the background? The process isn't necessarily suspended as soon as the app goes into the background and similarly it may come into the foreground while being still activated from the previous launch.
Are there any events in Javascript that can be used to execute code on foregrounding or backgrounding the app?
When the application is placed into the background, but not suspended (yet), you can use the "visibilitychanged" event, and "hidden" property on the document object:
http://msdn.microsoft.com/en-us/library/ie/hh773167(v=vs.85).aspx
These are fired the moment your app is no longer visible (e.g. the user hits the start button, or swipes to another application.
If your app is then suspended, you will also get the appropriate suspend events then.
From my understanding, as soon as a metro app completely looses focus (goes completely off screen) it is given 15 seconds (I think) by the OS to run its OnSuspend code, before being stopped and suspended. This happens every time the whole app loses complete focus and the reverse happens when it gets focus again (the user switches back to it).
The code that you would to run in these foreground and background events should be put in the Suspension and Reactivation event handlers
In Metro-style apps, the application will suspend when the user switches away from it and resume when the user switches back to it. Metro-style apps run full-screen and immersive, so "suspend" is equivalent to what you are thinking of as "going into the background" and resuming is equivalent to coming into the foreground.
Once the suspend event is fired (when a user swipes away from your app), your app has 5 seconds to store off state. In the suspend mode, your main thread of execution is suspended but the app is still held in memory. The user may come back to your app shortly, and then the thread is resumed and that state that you stored is never used. But, at some point, Windows may have too much in memory and need to terminate a process. If your app is terminated, then you can use the state that you stored to restore your app to the way it was when it was terminated on the next time that it is run.
Check out these resources for more info:
* The different Application Execution states: http://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.activation.applicationexecutionstate.aspx
* Guidelines around this: http://msdn.microsoft.com/en-us/library/windows/apps/hh465088.aspx

UIWebView does not respond to calls in "applicationWillResignActivity"

I am developing a web-app in iOS using PhoneGap. I need to make a javascript function call when the application goes into the background.
I am calling the function from "applicationWillResignActivity" method, but its not executing till the app comes to the foreground next time. Any other non-javascript code is getting executed. This happens only when I press home button. In other cases, like pressing power button when the app is running, and pressing the home button twice, the javascript function gets executed.
Is there some constraint over UIWebView of such kind? Can someone please tell if there is a way to make javascript work when user presses Home button?
It seems there is no way to capture such event. So just to self-answer this question, that there indeed is no delegate method to run JS code when app goes into background.

Is there a way in javascript to detect if the unload event is caused via a refresh, the back button, or closing the browser? [duplicate]

This question already has answers here:
Identifying Between Refresh And Close Browser Actions
(13 answers)
Closed 6 years ago.
I am currently looking at the "unload" event of a window to try to determine how the "unload" event was triggered, but am having little success. Is there a way to determine how the javascript event was triggered?
Page Refresh
Back Button (or navigate away from the page)
Closing the Browser
Essentially I need to execute some code only when the browser window is being closed, not refreshed or navigated away from.
Purpose: When a customer does an update of our software, the update will redirect their first Internet request to an offer page. There is a button for a "Do Not Bother" option, but some users will simply close their browser. Upon closing the browser, I need to duplicate the "Do Not Bother" functionality so the user no longer gets redirected to the offer page. Simply attaching to the "unload" event will not work due to the different ways of leaving a page.
No, and if there was it would be browser dependent.
What kind of code are you trying to run when the user closes the page?
Is it to logout the user?
Then the user would not be logged out if the browser crashes or the network connection breaks (and probably not if the computer goes to sleep/hibernation mode).
If it is for logout-purposes you should probably use a timestamp variable at the server that gets updated with every request (or use a ajax-ping), and logout the user if it hasn't been seen for a specified time.
Update: Found this answer here at stackoverflow.
Yes, there is a solution!
I've designed a solution based on onBeforeUnload+onLoad events, HTML5 local storage and client/server communication. See the details on https://stackoverflow.com/a/13916847/698168.
I use a method of doing keyboard "sniffing", in that it looks for keydown's of "F5", "ctrl+r", "alt-f4", "backspace" and others, and if it finds them flowing through the keyboard event queue, it sets boolean variables appropriately to trap that status... then I use a "onbeforeunload" function handler, which tests against those boolean status variables to decide what to do.
You can even shut down various keyboard strokes (like "ctrl+n" or "F1" for instance) by using preventDefault(), bubbles=false and returnValue=false in your keyboard handling.
This stuff is not for the faint of heart, but its certainly doable with some persistence and lots of cross browser testing!

Categories