show-notice
hide-notice

Tuesday 2 July 2013

jQuery setInterval() Function Example in JavaScript




Introduction

Here I will explain how to use jQuery setInterval function with example. jQuery setInterval function is used to execute functions at regular intervals of time. Suppose if we want to execute one JavaScript function for every 10 seconds or for a specific time we can use jQuery setInterval function to achieve this functionality. 

Description:
  
In previous articles I explained jQuery Create Read Cookie Get Set Delete Cookies Example,jQuery Get Number of Facebook likes, Shares, Comments Count for Url or Website and many articles relating to JQuery, JavaScript, asp.net.. Now I will explain how to use jQuery setInterval() function with example. 


We can use setInterval function in different ways like as shown below

Method 1:

setInterval(yourfunction(), timeinterval);

function yourfunction() {
// your code here
}
Method 2

setInterval(function(){// your code here
}, timeinterval);
For complete example check below code


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery setInterval function example</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");
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"style="font:bold 24px verdana" />
</div>
<button onclick="changeColor();">Start Timer</button>
</body>
</html>

SHARE THIS POST   

0 comments :

Post a Comment

Design by Gohilinfotech | www.gohilinfotech.blogspot.com