views:

79

answers:

2

Hi Everyone,

I have a picture slideshow that works on a click of a button like so: ">". The button is a span with id "slideshowRight".

Is it possible to set a timer that every 5 seconds literally clicks the slideshowRight button and stops when the mouse hovers over the buttons and restarts when the mouse doesn't hover on them?

The hover-stop is not as crucial as the every 5-second click. I know jQuery has a .click() function.

Thanks, Amit

+1  A: 

check the setTimeout and setinterval functions. something like this should click it ever 5 seconds:

setInterval(function(){
  $("myButton").click();
}, 5000);

You then could clear and start the interval when the user hovers in and out, using the above and the clearInterval function

Tim Büthe
@Tim: I liked your way a lot because I easily understood it much more than David's way. However the clearInterval() wasn't working for me and David's way worked. Sorry
Amit
@Amit: clear-interval should definitely work as well. you'd have to do something like this: `var slideshowInterval = null; function start() { slideshowInterval = window.setInterval(function() { myButton.click(); }, 5000); } myButton.hover(function() { /* on hover */ window.clearInterval(slideshowInterval); }, function() { /* stop hover */ start(); }); start();`
David Hedlund
@Amit: actually, here are both versions, side by side, for you to compare: http://jsbin.com/emuxa3/edit The reason I don't recommend calling `.click()` (or `trigger('click')`) is that you might want to assign some other code to the click listener of that button, to do something quite different, and you might *not* want to invoke that upon every 5 sec interval. you want the slideshow interval to change picture (or what it does) and *by coincidence* that's the same thing that the button does. if the behavior of the button changes, that doesn't mean the behavior of your interval should.
David Hedlund
Well there you go! heh, I really like your way as well. You're both right. I wish I could set both answers correct!
Amit
A: 

Shouldn't it be called slideshowLeft? Oh well,

If you have a button, then:

slideshowRight.click(function() {
    // do some stuff
});

you could actually trigger a click of that button by script:

slideshowRight.trigger('click');

but I'd argue it'd be neater to simply refactor the function:

slideshowRight.click(slideshowRight_click);

function slideshowRight_click() {
    // do some stuff
}

Based on that, you could set an interval to the following:

var isHover = false;
setInterval(function() {

    if(isHover) return;  // do nothing!

    slideshowRight_click();

}, 5000); // every 5 sec

And you could change the value of isHover like so:

slideshowRight.hover(function() { isHover = !isHover; });

Hover will be called once on hover, and once again when the user stops hovering the button, and its value will be set to whatever it wasn't at the moment.

David Hedlund
Haha you're totally right. Well I meant to write ">". My fault.
Amit