0

I am trying to write a user script that will hide certain elements from a page. The problem is the elements do not appear until a few seconds after the page has loaded, so I am trying to do it with a few seconds of delay.

This is the code I have:

function hide_stuff()
{
    var e = document.getElementsByClassName("tab");
    if(e) 
        alert("got elements");
    else 
        alert("didn't get elements");
    for( var i = 0; i < e.length; i++){
        if (!e[i].id)
            e[i].style.display = "hidden";
    }
}

setTimeout(hide_stuff(), 5000);

The problem is it doesn't delay at all. The "got elements" alert (which I added as a debugging aid), fires immediately when the page loads. I can't see what I'm doing wrong, although I'm sure it's probably something obvious.

Any help?

2
  • The problem is the elements do not appear until a few seconds after the page has loaded, are you using the window.onload event and running your code from there? Rarely is an arbitrary delay to let things load the right thing to do. Commented Mar 28, 2013 at 15:01
  • It's a user script, so it's not running on my own site; what if there is already a function assigned to the window.onload event? Wouldn't that cause problems if I assigned my own? Anyway, that doesn't seem to work in this instance. The elements are seemingly loaded dynamically after the page has loaded, so having it run with the window.onload event doesn't work. Commented Mar 28, 2013 at 15:53

1 Answer 1

4

Change

setTimeout(hide_stuff(), 5000);

to

setTimeout(hide_stuff, 5000);

Instead of passing the function, you were calling it immediately (and passing undefined to setTimeout).

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

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.