0

I get this error when i try to call this function in Chrome 16.0.912.77 m:

function fade(e){
    if(op > 0){
        op -= 0.01;
        document.getElementById(e).style.opacity = op;
        window.setTimeout("fade(\""+e+")\"", 10);
    }
}

It's a simple function which fade a element on my page out. I read that the error appears when i forgett a }-bracket, but i closed all..

Any ideas?

3
  • could you add the rendered html too? Commented Feb 1, 2012 at 8:59
  • and if you do setTimeout(function() { fade(e) },10);instead? Commented Feb 1, 2012 at 9:06
  • 2
    As an aside, you should avoid passing a string to setTimeout since it is a form of eval. See window.setTimeout at MDN for more. Commented Feb 1, 2012 at 9:09

2 Answers 2

2
window.setTimeout("fade(\""+e+"\")", 10);

You have the closing quote and closing parentheses swapped.

Sign up to request clarification or add additional context in comments.

1 Comment

Better code: setTimeout(() => fade(`"${e}"`), 10);
1

If you inspect this in your favourite JavaScript console:

var e = "foo";
alert("fade(\""+e+")\"");

... you'll see this:

fade("foo)"

Use on your favour the fact that JavaScript allows both single and double quotes:

var e = "foo";
alert('fade("' + e+ '")');

Or, even better, call setTimeout() with a function reference instead of a string (find some examples in the linked page).

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.