-1

I am trying to use setInterval and clearInterval in literally the simplest case possible:

var passiveInterval = "";
var activeInterval = "";

function myStartFunction()
{
    ...
    passiveInterval = window.setInterval(passiveCheck, pIntAmt);
    activeInterval = window.setInterval(activeCheck, aIntAmt);
    ...
}

function myEndFunction()
{
    ...
    if (passiveInterval != "")
    {
        alert("passiveInterval: " + passiveInterval);
        window.clearInterval(passiveItnerval);
        passiveInterval = "";
    }
    if (activeInterval != "")
    {
        window.clearInterval(activeInterval);
        activeInterval = "";
    }
    ...
}

The incredible thing is that the alert triggers, and gives me the correct value of the interval (an integer), but then the clearInterval statement triggers:

ReferenceError: Can't find variable: passiveItnerval

I have tried this with every permutation of window and this slapped on the front of everything, but nothing works...

Maxwell Collard
  • 2,587
  • 3
  • 16
  • 12
  • What is `passiveCheck` and when does `myEndFunction` get called? – James Montagne Feb 21 '12 at 02:17
  • `setTimeout` and `setInterval` return a +ve integer, so it is probably preferable to write: `if (passiveInterval) { /* clear timout */ }` rather than comparing with *empty string*. – RobG Feb 21 '12 at 02:27
  • @RobG That's a good idea, thanks. Also, is there any way to feel more like a tool than to post a typo here? > – Maxwell Collard Feb 21 '12 at 02:30

3 Answers3

2

Typo. crtl+f for "passiveItnerval"... the "t" and "n" are reversed.

Also, please consider using setTimeout instead of setInterval if possible. setInterval can get hairy.

Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
  • What's the best way of doing a recurring timer with `setTimeout` rather than `setInterval`? – Maxwell Collard Feb 21 '12 at 02:22
  • @MaxwellCollard just set another timeout at the end of the function triggered by the first timeout, and don't set another one instead of using clearInterval. – Dagg Nabbit Feb 21 '12 at 02:23
  • There's a [campaign to clean up Stack Overflow](http://meta.stackexchange.com/q/167342/187073) by removing these typo-related questions - we could really use your help! Would you mind pitching in a little by casting a close vote on this question? – VisioN Feb 28 '13 at 18:40
0

You've got a typo

passiveItnerval != passiveInterval

GoldenNewby
  • 4,382
  • 8
  • 33
  • 44
0

I have a simple function for this and you can use it over and over again:

var timer = function(t,i,d,f,fend,b) 
{
  if( !f )
   { return; }
  if( t == -1 || t > 0 )
   { setTimeout( function() { b=(f())?1:0; timer( (b)?0:(t>0)?--t:t, i+((d)?d:0), d, f, fend,b ); }, (b||i<0)?0.1:i ); }
  else if( typeof fend == 'function' )
        { setTimeout( fend, 1 ); }
};

parameters:
t = times/repeat count (-1 is endless)
i = interval wait period in milliseconds
d = Interval increase or decrease after each interval 
f = function to call after interval
fend = function to call when t ended

NOTE: when function f() returns true loop can be stopped even when it is endless

very simple to use:

timer(4,1000,0,function(){alert('called');},function(){alert('Thank you - finished');});

Imagine what you can do with it, it is endless ;-)

Greetz and cheers, Erwin Haantjes

Codebeat
  • 6,501
  • 6
  • 57
  • 99