For Developers

setInterval Method in Javascript

The setInterval() method evaluates a function or an expression repeatedly for the specified number of milliseconds and it returns an interval reference.

Syntax:


variable = setInterval(expression, milliseconds)
or
variable = setInterval(function, milliseconds)

variable holds the interval reference returned by seInterval method


Example:
Below example display the current time information using setInterval method call the date object.

< html>
< head>
< title> setInterval Method Example </title>
< script language="javascript">
function startSetInt()
{
ref=setInterval("init()",100);
}

function init()
{
dt = new Date();
document.getElementById("lbl").innerText = "Timer : " + dt;
document.getElementById("btnStart").disabled = true;
document.getElementById("btnStop").disabled = false;
}

function stopSetInt()
{
clearInterval(ref)
document.getElementById("lbl").innerText = "Timer : ";
document.getElementById("btnStart").disabled = false;
document.getElementById("btnStop").disabled = true;
}
</script>
</head>
< body onload="startSetInt()">
< label id="lbl" name="lbl"></label>
</br>
</br>
< input type="button" id="btnStart" value="Start" onclick="startSetInt()" />
< input type="button" id="btnStop" value="Stop" onclick="stopSetInt()" />
</body>
</html>

Demo : Click here

0 comments:

Post a Comment