user3537336
user3537336

Reputation: 221

"Uncaught TypeError: undefined is not a function" in my Google Chrome JavaScript console

So I understand the above error means my browser is not recognizing a function I'm attempting to call. My question is why this is happening in my particular instance. I'll post the relevant code and maybe you guys can help me.

I have a function

function homepage_screensaver()
{
    // ... bunch of lines here .... 
    var circwrapper = new Array(mycirc, theta);
    document.setInterval(move_obj(circwrapper, papwidth, papheight), 100);
}

which, as you can see, calls another function that I'm building right now:

function move_obj(obwrap, bckwid, bckht)
{
     var c = obwrap[1]; 
     var t = obwrap[2]; 
     var bb = c.getBBox(); 
     // ... bunch of lines here ...      
}

The error I'm getting is on the var bb = c.getBBox(); line.

I have a hunch. I think the problem has to do with me trying to wrap an object in an array and then unwrap it and call a function on it. Maybe the browser can't find the object's member function because of the fact that it has no idea what type of object it is in the first place.

(I know the fact that I created a wrapper class is functionally useless, but it is for my own readability.)

Anyways, what's the solution here?

Upvotes: 1

Views: 3438

Answers (1)

apsillers
apsillers

Reputation: 115910

Your problem is simply that obwrap[1] (which is theta outside the function) doesn't have a getBBox property. You probably intended to access obwrap[0] (which is mycirc). Also, your array doesn't have an index 2, so the following line should probably be changed to obwrap[1].

However, once you solve that problem, your code will immediately have another problem in a different place:

The way your code is currently written, setInterval will try to invoke the result returned by move_obj(circwrapper, papwidth, papheight). Your code is the same as:

var result_of_move_obj = move_obj(circwrapper, papwidth, papheight);
setInterval(result_of_move_obj, 100);

Note that move_obj is called only once, and then the result of move_obj is called periodically thereafter.

If the move_obj function ends with return undefined; (or not have a return statement, which is the same thing), then setInterval will try to invoke undefined, which obviously won't work.

Instead, you want to wrap your call inside of an anonymous function:

setInterval(function() { move_obj(circwrapper, papwidth, papheight) }, 100);

Upvotes: 2

Related Questions