Skip to content

Instantly share code, notes, and snippets.

@mwiegant
Last active October 3, 2015 00:40
Show Gist options
  • Save mwiegant/3f7b1fc6e6fe091d79d5 to your computer and use it in GitHub Desktop.
Save mwiegant/3f7b1fc6e6fe091d79d5 to your computer and use it in GitHub Desktop.
simple example timer, built for teaching a friend about web development
<head>
<title>Toggle Timer Page</title>
</head>
<body>
<p id="date">should not see this</p>
<br>
<p id="time">should not see this</p>
<br>
<p>Click button below to enable time updates.</p>
<button onclick="enable()">Enable updates</button>
<p>Click button below to disable time updates.</p>
<button onclick="disable()">Disable updates</button>
</body>
<script>
var interval; // will hold the interval id so that we can then stop the interval (if we wish)
// helper functions
function init() {
var date = new Date();
var milli = Date.now();
document.getElementById("date").innerHTML = "Today's date is " + date + '.';
document.getElementById("time").innerHTML = "There have been " + milli + " milliseconds since January of 1970.";
}
function enable() {
interval = setInterval( function() {
var date = new Date();
var milli = Date.now();
document.getElementById("date").innerHTML = "Today's date is " + date + '.';
document.getElementById("time").innerHTML = "There have been " + milli + " milliseconds since January of 1970.";
}, 250)
}
function disable() {
clearInterval(interval);
}
// call init() here so that it runs on every reload of this page
init();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment