Pages

Friday, 7 June 2013

How To Call a JavaScript Function at Regular Intervals Or Execute Function Every n Seconds in C# Asp.Net

How To Call a JavaScript Function at Regular Intervals Or Execute Function Every n Seconds in C# Asp.Net


Program:

.Aspx File:

<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;
    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>

Live Demo:

Run JavaScript function at specific intervals of time

No comments:

Post a Comment