1

I need to use:

var obj=document.getElementsByClassName[n];
setInterval("somefunc("+obj+");",10);

When I'm trying to run this code, I get "Uncaught SyntaxError: Unexpected identifier". I know about this problem with setTimeout("alert("+str+");), when I should use .toString(), but what if I need to pass an object in function?

Thank you.

3 Answers 3

2

Yes you can but like this:

setInterval(function(){
   somefunc(obj);
},10);

So here is how your code should be:

var obj = document.getElementsByClassName(n);
setInterval(function(){
   somefunc(obj);
},10);

You had these problems with your previous code:

  • You were calling your function immediately by passing param eg someFun(obj)
  • You were using eval() function behind the scenes by wrapping your code in quotes.
Sign up to request clarification or add additional context in comments.

Comments

1
setInterval(function () { somefunc(obj) }, 10);

Comments

1

you can use it like

var obj=document.getElementsByClassName[n];
setInterval(function() {return somefunc(obj)},10);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.