How to set timer in javascript?How to call javascript function continuously?

Here I am going to explain How to set timer in javascript?How to call javascript function continuously?. We can use functionality whenever we want to execute a certain function repeatedly or with the duration of some period of time. So here in this I am creating a method which will accept two parameter  setInterval(functionname, timedelay)..

 

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Http://dotnetstock.com</title>
<script type="text/javascript">
var count=0;
function dotnetstock() {
// Call function with 1000 milliseconds gap
setInterval(starttimer, 1000);
}
function starttimer() {
count += 1;
var oElem = document.getElementById("divtxt");
oElem.style.color = oElem.style.color == "red" ? "blue" : "red";
document.getElementById("lbltxt").innerHTML = "Your Time Starts: "+count;
}
</script>
</head>
<body>
<div id="divtxt">
<label id="lbltxt"  />
</div>
<button onclick="dotnetstock();">Start Timer</button>
</body>
</html>

 

1 Comment

  1. airish delas alas

    thanks for this! 🙂 a big help

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *