Last active
August 29, 2015 14:16
-
-
Save tudo75/b38fedbd59a66d81f927 to your computer and use it in GitHub Desktop.
setInterval clearInterval Javascript demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<body> | |
<p>Clock start on page load and stop after 5 seconds:</p> | |
<p id="demo"></p> | |
<p>Clock start on page load and stop when you click button:</p> | |
<p id="demo1"></p> | |
<button onclick="myStartFunction()">Restart second clock</button> | |
<button onclick="myStopFunction()">Stop second clock</button> | |
<script type="text/javascript" src="script.js"> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myVar = setInterval(function(){ myTimer(); }, 1000); | |
var i = 0; | |
function myTimer() { | |
var d = new Date(); | |
var t = d.toLocaleTimeString(); | |
document.getElementById("demo").innerHTML = t; | |
i++; | |
if (i == 5) | |
clearInterval(myVar); | |
} | |
var myVar1 = setInterval(function(){ myTimer1(); }, 1000); | |
function myTimer1() { | |
var d = new Date(); | |
var t = d.toLocaleTimeString(); | |
document.getElementById("demo1").innerHTML = t; | |
} | |
function myStopFunction() { | |
clearInterval(myVar1); | |
} | |
function myStartFunction() { | |
myVar1 = setInterval(function(){ myTimer1(); }, 1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment