How to Call a Javascript function at equal Interval in ASP.Net ?
Sometimes, we may get a situation where we need to call a client side javascript function at equal intervals. Moving forward, this little code snippet will help us to do that.
The javascript window object has a inbuilt method called setInterval() which can be used to achieve this. Refer the below code,
<script type="text/javascript"> var fnInt = setInterval(CallMe, 3000); function CallMe() { alert("Hello!!"); } </script>
The above script will call the function CallMe() at every 3000ms untill we cancel this scheduled call using clearInterval() function of window object.
How to cancel/clear the Javascript function call made using setInterval() ?
The setInterval() function will return the ID of the scheduled call when a function is scheduled. Hence, in oreder to cancel the function call, we need to call the clearInterval() function by passing this ID. Refer the below code,
<script type="text/javascript"> var fnInt = setInterval(CallMe, 3000); function CallMe() { alert("Hello!!"); }
function AbortJSCall() { clearInterval(fnInt); } </script> </head> <body> <form id="form1" runat="server"> <input type="button" value="Abort JS Call" onclick="AbortJSCall()" />
Happy Coding!!
|