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

Call a JavaScript Function at Regular Intervals | Execute Function Every n Seconds

Feb 5, 2013

Here I will explain how to call a JavaScript function at regular intervals of time or run/execute JavaScript function every 10 seconds or execute JavaScript function at specific time in

In previous articles I explained
jQuery get current page url and title, jQuery get url parameter values, Create tabbed Menu with rounded corners using CSS, jQuery shake image on mouse hover and many articles relating to JavaScript, jQuery, Now I will explain how to call a JavaScript function at regular intervals of time in

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. If you want to see it in example you need to write the code like as shown below

<html xmlns="">
<title>Run JavaScript function at specific intervals of time</title>
<script type="text/javascript">
var count=0;
function changeColor() {

// Call function with 500 milliseconds gap
setInterval(starttimer, 500);
function starttimer() {
count += 1;
var oElem = document.getElementById("divtxt"); = == "red" ? "blue" : "red";
document.getElementById("lbltxt").innerHTML = "Your Time Starts: "+count;
<div id="divtxt">
<label id="lbltxt" style="font:bold 24px verdana" />
<button onclick="changeColor();">Start Timer</button>
Live Demo

For live demo click on below button

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

Get the latest,, 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


Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

I want something where the times get refresh on every click. Here in the above example if you click n number of time there will be n instances and the increment go mad.......

Unknown said...

i want to stop this timer control after some specified time, how can i do that?

Give your Valuable Comments

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

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