T110
T110

Reputation: 67

setInterval() issue in Mozilla (Javascript function)

I'm running through JavaScript: the Definitive Guide
It offers up the following code to explain setTimeout() and setInterval(), and my issue is that it runs in Safari without issue but in Mozilla it doesn't seem to trigger at all, anyone have any thoughts?

The issue is in the following function:

function invoke(f,start,interval,end){
        if(!start) start=0; //default to 0ms (start right away)
        if (arguments.length <= 2)
            setTimeout(f,start);

It functions if I don't set the inverval and end, but if I do something goes janky

    else{
        setTimeout(repeat,start);
        function repeat(){
        var h = setInterval(f,interval);
        //if(end)setTimeout(function(){clearInterval(h)},end);
        }
    }
    }

This is just the dummy function that runs on setTimeout() and setInterval()

    function f(){
    if(true)
        alert("yo");
    }



<button onclick="invoke('f,200,1000,5000')">yo</button>

Hopfully somone has some insight into this one, thanks.

Upvotes: 0

Views: 688

Answers (3)

Dampsquid
Dampsquid

Reputation: 2474

JSFiddle this now appear to work,

as the others have said you need to remove the 's arround your parameter to invoke

also FireBug for firefox ( get it if you dont already ) fails with Repeat is undefined so I've also modified that a little too.

Upvotes: 0

Grant Zhu
Grant Zhu

Reputation: 3018

<button onclick="invoke('f,200,1000,5000')">yo</button>

should be

<button onclick="invoke(f,200,1000,5000)">yo</button>

Otherwise you are passing the string 'f,200,1000,5000' as the first parameter.

Upvotes: 1

SenorAmor
SenorAmor

Reputation: 3345

It looks like you're passing a single variable to your invoke function due to the placement of your second single quote. Try changing it to

<button onclick="invoke('f',200,1000,5000)">yo</button>

and see if that works any better.

Upvotes: 0

Related Questions