what happens if I create timers using setInterval() repeatedly

Hi All,

I would like to know whether javascript timers, created using setInterval function can cause problem if clearInterval is not called.
for eg look at this code
var i=0;
function increment()
{
i++;
}
function startTimers()
{
for (var i=0;i<2000;i++)
setInterval("increment()",30000);
}

here timers are created repeatedly but never cleared using clerInterval().
My question is will the code like this will effect the systems performance where this script is running??
Actually my application has a java script function which reloads a java servlet in a hidden frame. And this refresh should happen say after every 30 seconds. So I started a timer using setInterval.
But CPU utilization is continously showing 100%. When I looked at the code hoping to find out the problem, i found a pattern some thing like this in my code::

function callRefresh()
{
//some other code here
//refresh hidde frame here using location.replace()
setInterval("RefreshPage()",30000);
}

function RefreshPage()
{
if (some condition)
callRefresh();
}

bcoz of this page is being refreshed but number of timers are created. Will this cause problem of "CPU utilization"??

Any knd of help regarding this issue will be appreciated

thank u

Comments

  • : Hi All,
    :
    : I would like to know whether javascript timers, created using setInterval function can cause problem if clearInterval is not called.
    : for eg look at this code
    : var i=0;
    : function increment()
    : {
    : i++;
    : }
    : function startTimers()
    : {
    : for (var i=0;i<2000;i++)
    : setInterval("increment()",30000);
    : }
    :
    : here timers are created repeatedly but never cleared using clerInterval().
    : My question is will the code like this will effect the systems performance where this script is running??
    : Actually my application has a java script function which reloads a java servlet in a hidden frame. And this refresh should happen say after every 30 seconds. So I started a timer using setInterval.
    : But CPU utilization is continously showing 100%. When I looked at the code hoping to find out the problem, i found a pattern some thing like this in my code::
    :
    : function callRefresh()
    : {
    : //some other code here
    : //refresh hidde frame here using location.replace()
    : setInterval("RefreshPage()",30000);
    : }
    :
    : function RefreshPage()
    : {
    : if (some condition)
    : callRefresh();
    : }
    :
    : bcoz of this page is being refreshed but number of timers are created. Will this cause problem of "CPU utilization"??
    :
    : Any knd of help regarding this issue will be appreciated
    :
    : thank u
    :
    :
    This entirely depends on the precise implementation of setInterval in the browser.
    There is however another method, which could be helpful. This method uses the setTimeout() function. An example is here: http://developer.irt.org/script/1238.htm
  • setInterval() is similar to setTimeout(), except that it keeps running - setInterval('func()',3000) will execute func every 3 seconds, whereas setTimeout('func()',3000) wil only do it once, in three seconds.

    [code]var i=0;
    function increment()
    {
    i++;
    }
    function startTimers()
    {
    for (var i=0;i<2000;i++)
    setInterval("increment()",30000);
    }[/code]This would increment the value of i by 2000 every five minutes (for 30 seconds you only want 3000 ms), because you set 2000 intervals. I don't know how much CPU it would use, but all of it seems perfectly believable. If you want to run incrememnt every five minutes for the next 10000 minutes you'd have to set one and clear it after running the function 2000 times, eg (untested)
    [code]var i = 0;
    var hasrun = 0;
    function increment() {
    i++;
    hasrun++;
    if (hasrun >= 2000)
    clearInterval('int');
    }
    var int = setInterval('increment()',30000)[/code]

    If, as I think is more likely, you just didn't think it conceivable that somebody would be on the page for more than 10000 minutes, it's even easier - just don't bother with line 2, or 5-7. Nor do you need the "var int = " when setting the interval.

    Without being able to see the entirety of your code I can't be sure, but
    [code]function callRefresh() {
    if (some condition) {
    //some other code here
    //refresh hidde frame here using location.replace()
    }
    }
    setInterval("callRefresh()",3000);[/code] should work. Or as Zibadian suggested you could use setTimeout(), but intervals are cleaner and in all probability use less memory.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories