Multiple clicks

1

I have the following code snippet:

var Tannerie = {

    init : function(){

        $('#prev').mouseover(Tannerie.prevClick);

        $('#next').mouseover(Tannerie.nextClick);

    },

    prevClick : function(){
        $(this).click();        
    },

    nextClick : function(){
        $(this).click();
    }   

}

$(Tannerie.init);

That is, when I hover the mouse over a particular div it simulates a click. But I wanted to make it, when leaving the mouse on the div, that is giving continuous clicks. I tried to use setInterval but it did not work, but I do not think I did it right.

    
asked by anonymous 06.03.2014 / 13:46

2 answers

2

You can use the .hover() function / event together with setInterval :

// Cria uma variável para armazenar a referência ao intervalo:
var intervalo;

$('seletor').hover(function () {
  // Caso o mouse entre no elemento começar a clicar:
  intervalo = setInterval(function () {
    $('seletor').click();
  }, 150); // o número é o tempo em milisegundos entre cada clique
}, function () {
  // Caso o mouse saia do elemento parar de clicar:
  clearInterval(intervalo);
});

Replace seletor with what you are using. I recommend placing the element in a variable (cached) so that the functions are executed with better performance.

I left it more generically if you wanted to organize the code as you want, such as putting the element, interval, and time between clicks as properties of the object Tannerie .

    
06.03.2014 / 14:01
0
while($('#elem').is(":hover")){
    $('#elem').trigger('click');
}

I have not tested this, but should work =)

I've taken from here for any questions.

    
06.03.2014 / 13:49