views:

62

answers:

3

Hello, I want that when mouse is over an image, an event should be triggered ONCE, and it should be triggered again only after mouse is out of that image and back again, and also at least 2 seconds passed.

If I leave my mouse over the image,it gets called like every milisecond,and by the logic of my function once you hover on the variable 'canhover' becomes 0 until you move mouse out

This code seems to have a bug and I cant see it. I need a new pair of eyes, but the algorithm is kinda logical

Working code :

<script type="text/javascript">
var timeok = 1;
function redotimeok() {
    timeok = 1;
}
//
function onmenter()
{
if (timeok == 1) 
    {
  enter();
  timeok = 0;
    }
}
//
function onmleave()
{
  setTimeout(redotimeok, 2000);
  leave();
}
//

$('#cashrefresh').hover(onmenter,onmleave);

function enter(){
  $("#showname").load('./includes/do_name.inc.php');
  $("#cashrefresh").attr("src","images/reficonani.gif");
}

function leave(){
  $("#cashrefresh").attr("src","images/reficon.png");
}
</script>
+1  A: 

I don't know if this will solve your entire problem (since we don't have a detailed description of what it is), but instead of:

$('#cashrefresh').hover(onmenter(),onmleave());

try:

$('#cashrefresh').hover(onmenter,onmleave);

And the same thing here:

setTimeout(redotimeok, 2000); // just the function name

Also, I don't see where you ever set timeok to zero. Do you mean to set timeok = 0 in onmenter()?

Ken Redler
Thanks for trying,but it didnt help. If I leave my mouse over the image,it gets called like every milisecond,and by the logic of my function once you hover on canhover becomes 0 until you move mouse out.
nevergone
Try fixing the `settimeout` too.
Ken Redler
+1 This is most likely the correct answer :) Also note that `leave();` is called twice in `onmleave`, which might or might not be intended. I would assume that `timeok = 0` should be set in `onmleave` (it would make more sense).
Felix Kling
i fixed all that
nevergone
Obviously I can't run your code, but it seems like what you want is to leave `timeok` as `1`, and then set it to `0` as soon as the user mouses over -- *not* two seconds after the mouseout. If you set `timeok = 0` in `onmleave`, the mouseover makes your `enter()` function off limits until `timeok` is `1` again -- two seconds after the mouseout. Unless I'm reading this wrong -- very possible -- that's what you're after.
Ken Redler
I call function onmleave and inside that I set timeout for fc that sets timeok to 1 after 2 seconds that mouseout event occurs.
nevergone
You can set it to 0 on the enter or the leave. The key is to set it *back to 1* in the function aptly named redotimeok. Make timeok be *off* by the time you leave, and then turn it back *on* two seconds later. As written, redotimeok is setting it to zero.
Ken Redler
A: 

Try changing onmleave function as follows:

   function onmleave()
    {
      setTimeout(redotimeok, 2000);
      leave();
    }
eapen
You have to omit the `()`.
Felix Kling
Good catch, thanks Felix! I corrected it.
eapen
+1  A: 

There are two methods in jquery for your problem:

.mouseenter() and .mouseleave()

Check out the demos there.

EDIT:

I thought hover was for mouseover and mouseout, sorry for confusion.

I checked your code again. And it seems that you're changing the image when mouse gets over the image, which forces browser to load the new image and the old image disappears for a very little while till the new one appears and i think this must be triggering both handlers continuosly and you're getting this behaviour.

Try not to change the source of the image, comment out that line and instead console.log("some message") there and see if the message is repeated as much as .load() was fired before.

Hope this helps.

Sinan Y.
Doesn't hover use mouseenter and mouseleave (instead of mouseover and mouseout)? That's what I got from the docs.
eapen
sorry for confusion, edited.
Sinan Y.