Change, update
Note. div is not a valid child of p . Removed p elements that were parents of a div element
html
<!DOCTYPE html> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" /> <script src="http://code.jquery.com/jquery-1.11.1.min.js"> </script> <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"> </script> <script src="script.js"></script> <body> <div data-role="page" id="page1"> <div data-role="header"> <h1>Page1</h1> </div> <div data-role="content">This is page 1</div> <div data-role="footer"> <h4>Footer</h4> </div> </div> <div data-role="page" id="page2"> <div data-role="header"> <h1>Page 2</h1> </div> <div data-role="content">This is page 2</div> <div data-role="footer"> <h4>Footer</h4> </div> </div> <div data-role="page" id="page3"> <div data-role="header"> <h1>Page 3</h1> </div> <div data-role="content">This is page 3</div> <div data-role="footer"> <h4>Footer</h4> </div> </div> <div data-role="page" id="page4"> <div data-role="header"> <h1>Page 4</h1> </div> <div data-role="content">This is page 4</div> <div data-role="footer"> <h4>Footer</h4> </div> </div> </body> </html>
If hash of location starts with #page1 , you can use Number() , String.prototype.slice() to enlarge pages using existing js .
Defined by mousemove , the keypress event handler as a named function, substituting .one() for .on() to prevent the handler from being called in every mousemove event; defined goToNextPage variable outside the .one() handler
Js
$(function() { location.href = "#page1" var goToNextPage = null; function updatePage(event) { console.log(event) var currentPage = Number(location.hash.slice(-1)); // if `currentPage` is less than 4 if (currentPage !== 4) { goToNextPage = setTimeout(function() { if ((currentPage + 1) <= 4) { // set `location.href` to `#page` + `currentPage` + `1` location.href = "#page" + (currentPage + 1); console.log(location.hash); // reset event handlers $(document).one("mousemove keypress", updatePage); } }, 3000); } else { clearTimeout(goToNextPage) } } $(document).one("mousemove keypress", updatePage); });
plnkr http://plnkr.co/edit/tunX9CjEqNEZWxoxXU1b?p=preview
source share