shadowayex Posted October 12, 2009 Share Posted October 12, 2009 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 More sharing options...
Synook Posted October 12, 2009 Share Posted October 12, 2009 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 More sharing options...
shadowayex Posted October 12, 2009 Author Share Posted October 12, 2009 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 More sharing options...
justsomeguy Posted October 12, 2009 Share Posted October 12, 2009 How about something like this:setInterval(function() { funcEx(this, 1); }, 1000); Link to comment Share on other sites More sharing options...
shadowayex Posted October 12, 2009 Author Share Posted October 12, 2009 You know, that might actually work. I'll give it a shot and come back with the results. Link to comment Share on other sites More sharing options...
Synook Posted October 12, 2009 Share Posted October 12, 2009 I don't think support for passing function references in the first argument of setIntveral() is too great... not sure of exact compatibility though. Link to comment Share on other sites More sharing options...
shadowayex Posted October 13, 2009 Author Share Posted October 13, 2009 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 More sharing options...
Synook Posted October 13, 2009 Share Posted October 13, 2009 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. Link to comment Share on other sites More sharing options...
shadowayex Posted October 13, 2009 Author Share Posted October 13, 2009 Oh, now it works out nicely =D. Thanks. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 13, 2009 Share Posted October 13, 2009 You could also just write it like this, although I'm not sure what the object will be scoped to:setInterval("funcEx(this, 1)", 1000); Link to comment Share on other sites More sharing options...
shadowayex Posted October 13, 2009 Author Share Posted October 13, 2009 I'll see if I can chop it to that as well. The less code, the better . Thanks for your ideas guys. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.