Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Start and Stop a Timer in JavaScript | Function Execution Start and Stop in JavaScript

Feb 5, 2013
Introduction

Here I will explain how to start and stop timer example in JavaScript or start and stop function execution using JavaScript in asp.net.

Description:
  
In previous articles I explained Execute JavaScript function at regular intervals of time
jQuery get current page url and title, jQuery shake image on mouse hover and many articles relating to JavaScript, jQuery, asp.net. Now I will explain how to implement start and stop timer example in JavaScript.

To execute function repeatedly with fixed time delay we have a function with two parameters called setInterval(functionname, timedelay)

In this function we need to call the required function in functionname field and we need to set the required time delay to execute the function in timedelay field.

To stop the execution of repeated action we have function clearInterval(intervalID)
Here intervalID is the identifier of the repeated action you want to cancel. This ID is returned from setInterval().

If you want to see it in example you need to write the code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Run JavaScript function at specific intervals of time</title>
<script type="text/javascript">
var count=0,chkfn=null;
function changeColor() {
// Call function with 1000 milliseconds gap
chkfn = setInterval(starttimer, 1000);
}
function starttimer() {
count += 1;
var oElem = document.getElementById("divtxt");
oElem.style.color = oElem.style.color == "red" ? "blue" : "red";
document.getElementById("pcount").innerHTML = "Your Time Starts: " + count;
}
function stoptimer() {
clearInterval(chkfn);
chkfn = null;
count = 0;
document.getElementById("pcount").innerHTML = '';
}
</script>
</head>
<body>
<div id="divtxt">
<p id="pcount" style="font:bold 24px verdana"></p>
</div>
<button onclick="changeColor();">Start Timer</button>
<button onclick="stoptimer();">Stop Timer</button>
</body>
</html>
Live Demo

For live demo click on below buttons



If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

6 comments :

Unknown said...

i need duration between login and logout time

Pankaj Tiwari said...

Hi Suresh !!!!

I need your urgent help please !!!

I have requirement to sync Telerik calender with Google Calender, Outlook and ical.....

Can you give me any idea please.... Or you can reply me on my id - pankaj.ics@gmail.com

Unknown said...

ASP.NET website counter

asp.net at realtime when multiple users are using the site it increments its count in database or any file but it good looks also

and plz reply me your link post when you made website counter

rohit said...

Hi,I am working on start stop pause timer.my project vb.net vs2005(employee management system)when employee start some work he just start the timer and that time time of the system start as a lable on the lable or in textbox and same time store in database .when his work has been done he stop the timer then timer of the lable will stop and same time store in database,can you please help me to do that give me something related to that

rohit said...

plz reply me sir rsrohitsharma206@gmail.com

Unknown said...

Hi how to stop execution java script function on client click.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.