How do i repeat a trigger continuously on mouseover or hover?
I have the following Script, which kind of works.
If i move my mouse cursor over a link in '.sample', a 'click' is being triggered.
How can i achieve, that this click is continuously triggered (kind of a loop)?
$jq('.sample a').mouseover(function(){
$jq(this).trigger('click');
return false;
});
I tried the following, which is not working (the click is triggered only once and then it stops):
$jq('.sample a').mouseover(function(){
setInterval(function() { $jq('.nav-sub-browser a').trigger('click'); }, 100);
});
I also tried the following solution, which i found at stackoverflow. But this one too triggers the c开发者_StackOverflowlick only once:
function triggerClick() {
$jq('.nav-sub-browser a').trigger('click');
}
var interval;
$jq('.nav-sub-browser a').hover(function() {
interval = setInterval(triggerClick(), 100);
},
function() {
clearInterval(interval);
});
What am i doing wrong?
Have you tried just click? Or is there a specific reason you're using trigger?
var myInterval = false;
$('.sampleA').mouseover(function(){
myInterval = setInterval(function(){
$('.nav-sub-browser a').click();
}, 100);
});
$('.sampleA').mouseout(function(){
clearInterval(myInterval);
myInterval = false;
});
Here's a jsFiddle that demonstrates that: http://www.jsfiddle.net/jbenson/dpC7W/
You need to pass a function reference to setInterval
, so get rid of the trailing ()
after triggerClick
:
function triggerClick() {
$jq('.nav-sub-browser a').click();
}
var interval;
$jq('.nav-sub-browser a').hover(function() {
interval = setInterval(triggerClick, 100);
}, function() {
clearInterval(interval);
});
精彩评论