Hi All,
I have web page where the datas are refreshed by calling Asp.net web service from javascript. I am using javascript setInterval() for updating the datas periodically, also using clearInterval() for stopping this javscript timer. In total the page gets details from 3 different web services. Once the data is received the setinterval delay will be set to 20 seconds(initial setInterval delay is 2 seconds). But some times even after receiving the data and setting the setinterval delay to 20 seconds, setInterval calls continuously. Please help me to solve this issue.

Thanks

Dani AI

Generated

A few likely causes and quick checks that explain why a timer keeps "running" even after calling clearInterval: most often the page has multiple intervals active (clearing one leaves others), or the code is clearing the wrong/undefined id because the timer variable is redeclared or out of scope. 's demo shows the basic clear behavior for a single timer, but in a real app that starts timers from several places (multiple web-service callbacks, repeated initialization on partial postbacks, or event re-binding) the symptoms match what describes.

Checklist to try before changing architecture

  • Search the codebase for every setInterval and make sure each call stores its return value in a single, shared variable you clear before reassigning.
  • Avoid var timer inside nested functions that shadow a global timer used by clearInterval.
  • Log the id returned by setInterval and the id passed to clearInterval (use console.log) to confirm they match.
  • Remember alerts can hide timing issues; they don't stop other intervals from existing.
  • If using ASP.NET UpdatePanel or other partial postbacks, initialization code may run again after an async postback — guard initialization with a flag so a timer is started only once.

A safer pattern: schedule the next poll at the end of the Ajax callback (single timer at any time). This prevents overlapping calls and accidental multiple timers:

var pollTimer = null;

function poll() {
  // make ajax call, then in its success/error callback:
  // pollTimer = setTimeout(poll, 20000);
}

function startPolling() {
  if (pollTimer) { clearTimeout(pollTimer); pollTimer = null; }
  poll();
}

function stopPolling() {
  if (pollTimer) { clearTimeout(pollTimer); pollTimer = null; }
}

Debug tips: instrument the start/stop points with console.log("start", id) and console.log("stop", id), search for repeated event bindings (jQuery .bind/.on inside functions), and test in another browser to rule out environment-specific quirks. The simplest durable fix is to centralize the timer id, clear it before creating a new one, or switch to the chained setTimeout pattern shown above.

Recommended Answers

All 3 Replies

Here's a little demo about clearing the timer in the given funtion:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<HTML lang="en">
<HEAD>
<META http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<TITLE>Clear Interval DEMO</TITLE>
<SCRIPT type="text/javascript">
<!--
var time, timer, counter;
counter = 21;

Function.prototype.count = function( DELAY ) {
return setInterval( this, DELAY );
}

timer = function() {
   if ( counter ===  0 ) { 
    clearInterval( time );
      { return alert( "The timer has been cleared!" ); 
      } 
   } div = ( document.getElementById ) ? document.getElementById("output") : document.all.output; 
   return div.innerHTML = ( counter -= 1 );
}

window.onload = function() {
time = timer.count( 1000 );
}
</SCRIPT>
</HEAD>
<BODY>
<DIV ID="output"></DIV>
</BODY>
</HTML>

Essential,
My issue is even after clearing the timer the timer doesn't stops.
For checking this particular issue i put a button which fires the
clearInterval() on clicking. Then i simulate the condition again,
when the issue occures again i clicked the button for clearing the
timer,also i put an alert box before and after the clearInterval(), still
the timer is not stoping.
Thanks

It will be easy for us to provide you the exact script that you need, if you are willing to post the code concerning this issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.