setTimeout not working
I want something to run with onmouseover but not straight away - with a delay.
the javascript below does not seem to work:
a.onmouseover = function(){
var timer = setTimeout(this.nextSibling.style.display = 'block', 1000);
}
rather than delaying a second the elements style.display block appears immediately.
Any ideas?
decibel.places posted this at 20:01 — 24th December 2008.
He has: 1,494 posts
Joined: Jun 2008
you need to define the function properly, add an event listener to your anchor tag, and pass "this" to the function
let's define the function:
function nextSibling(elm){
var timer = setTimeout(elm.nextSibling.style.display = 'block', 1000);
}
now let's set up the anchor
<a href="somevalue" onmouseover="nextSibling(this)">linkcontent</a>
if it's not a real link, I recommend setting the
href="javascript:void(0)"
benf posted this at 22:44 — 25th December 2008.
They have: 426 posts
Joined: Feb 2005
hi thanks for your reply,
Im not quite sure what you mean "Define the function properly"?
The function works, just the setTimeout() does not.
Any other advise appreciated.
Good Value Professional VPS Hosting
decibel.places posted this at 18:30 — 28th December 2008.
He has: 1,494 posts
Joined: Jun 2008
The function works, just the setTimeout() does not.
are you saying that this works (without the timer)?
a.onmouseover = function(){
this.nextSibling.style.display = 'block';
}
I do not think your syntax is correct, first of all using a variable like a.onmouseover, secondly when you declare a function as a variable this way, you still need to define the variable(s) being passed to the function eg
var add=function theAdd(a, b)
{
return a+b;
}
alert(add(1,2)); // produces 3
alert(theAdd(1,2)); // also produces 3
(refer to this page).
I would do something like
function name(){
somecode
}
var theelm = name; //not sure if you need the parentheses here or not...
but I am uncertain of the value of assigning the function to a variable
Notice in my example above that I am passing the element object with the mouseover to the function as this/elm
benf posted this at 21:17 — 2nd January 2009.
They have: 426 posts
Joined: Feb 2005
Hi Decibel,
Yes the
a.onmouseover = function(){
this.nextSibling.style.display = 'block';
}
Does work.
Funny thing is that if i dont declare a.onmouseover as a function then when the page loads whatever i want a.onmouseover to do executes as the page loads, but not if i declare it as a function.
How can i pass parametres into the function if i should do this:
function something(){
}
a.onmouseover = something;
is this acceptable: a.onmouseover = something(a);
Good Value Professional VPS Hosting
decibel.places posted this at 21:40 — 2nd January 2009.
He has: 1,494 posts
Joined: Jun 2008
I think the code breaks down when the function does not know what "this" refers to.
In my first response on the thread, I showed you how to pass a particular anchor object (this) to the function nextSibling(elm) - note that "this" (the anchor object) becomes "elm" in the function
If you put the code in without declaring it as a function, it will execute when it loads. If it is a function, the function needs to be called.
If you can post a link to the page, or attach an html file, we can test our solutions...
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.