Jump to content

Passing Objects Through Setinterval() Functions


shadowayex
 Share

Recommended Posts

I'm having an interesting problem with a new project of mine. I'm wanting to use setInterval, and it involves a function that takes parameters. Not a problem, right? Eh, it's become one. The deal is, the function takes two parameters, one that is an object (via this) and another that is a number. The number passes fine. The problem come with passing the object. Firefox's error console describes it as a missing ] or something.The way I'm trying to do this is:setInterval("funcEx(" + this + ", 1), 1000);I've tried different variations of that, but each one yielded different errors, most of which just told me that I was doing it wrong anyway. This one, however, seems as though it should be right. It just doesn't work.Is there any way to pass objects as parameters through setInterval?

Link to comment
Share on other sites

Your problem is that objects aren't scalar, so when you try to append it to the string for passing, it doesn't understand what to do. You have to find some other way to indicate what you want passed (e.g. an ID).

Link to comment
Share on other sites

Ehhhh sad face. I suppose that'd work. The problem is I was trying to use list items to get teh parentNode, as there's multiple lists, and effect only that list's parent. using IDs would mean I'd have to set each with a static ID (like list1-1, list1-2, list2-1, and so on), and do it that way. That's not fun =/

Link to comment
Share on other sites

I don't think support for passing function references in the first argument of setIntveral() is too great... not sure of exact compatibility though.

Edited by Synook
Link to comment
Share on other sites

I think you are right Synook. Error console screams syntax error >_<. It was such an ingenious idea. I thought for sure it'd work.This is what I did for a test:setInterval("function() { document.getElementById('para').innerHTML += 'function loaded '; }", 1000);Seems straight forward to me. The error console has the arrow pointed at the parentheses on function().

Link to comment
Share on other sites

If you try JSG's idea, don't put quotes - or else you end up in the same situation again. With JSG's method, you are calling:

setInterval(Object, int)

instead of

setInterval(String, int)

So just

setInterval(function() { document.getElementById('para').innerHTML += 'function loaded '; }, 1000);

I think the compatibility issues are mostly with older versions of IE.

Edited by Synook
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...