I am working on an app with jQuery mobile. I have a script that I want to start once the user enters a page and stops once they leave.
I have used:
$( document ).delegate("#myPage", "pagecreate", "", function() {
//my script here
});
to have the script only run once the user enters “myPage.” How can I have it so the script stops once they leave “myPage”?
Edit: I am using ajax-based navigation so it is all one HTML file. I am trying to start and stop a script when someone enters a new subpage (#page).
You can use the
pageshowandpagehideevents for a specific pseudo-page. Something like this:Here is the documentation for jQuery Mobile Events, there is a whole section on page-events: http://jquerymobile.com/demos/1.1.0/docs/api/events.html
Using
pageshow/pagehidehas the advantage that the code will run on subsequent views of the pseudo-page as well, not just on the first page-load.