Skip to content

Instantly share code, notes, and snippets.

@cfjedimaster
Created April 13, 2012 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfjedimaster/2377854 to your computer and use it in GitHub Desktop.
Save cfjedimaster/2377854 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<script src="cordova-1.6.0.js"></script>
<script>
var startTime;
function init() {
console.log("init");
document.addEventListener("deviceready",startup,false);
}
function startup() {
console.log("startup");
if(!localStorage["totalMinutes"]) localStorage["totalMinutes"]=0;
startTime = new Date();
document.addEventListener("pause",pauseHandler,false);
document.addEventListener("resume",resumeHandler,false);
displayTimeUsed();
}
function pauseHandler(e) {
console.log("Running pause");
//everytime we pause, we if startTime is valid, get the difference in minutes, and add
if(startTime instanceof Date) {
var rightNow = new Date();
var diff = rightNow.getTime() - startTime.getTime();
var minuteDiff = Math.floor((diff/1000)/60);
var current = parseInt(localStorage["totalMinutes"]);
localStorage["totalMinutes"] = current+minuteDiff;
}
}
function resumeHandler(e) {
console.log("Running resume");
startTime = new Date();
displayTimeUsed();
}
function displayTimeUsed() {
document.querySelector("#status").innerHTML = "<p>Total number of minutes used with this application is "+localStorage["totalMinutes"]+".</p>";
}
</script>
</head>
<body onload="init()">
</body>
<h2>Usage</h2>
<div id="status"></div>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment