Multiple clicks

Asked

Viewed 255 times

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 pass the mouse by a certain div it simulates a click. But I wanted to make sure that, by leaving the mouse on the div, that it keeps giving continuous clicks. I tried to use setInterval but it didn’t work, but I guess I didn’t do it right.

2 answers

2

You can use the event/function .hover() together with the 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);
});

Substitute 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 generic if you want to organize the code as you want, like placing the element, interval and time between clicks as properties of the object Tannerie.

0

while($('#elem').is(":hover")){
    $('#elem').trigger('click');
}

I didn’t test it, but it should work =)

I took from here, for any doubt.

  • 2

    The ideal would be to put a setInterval and put a if($('#elem').is(":hover")) and in the else disable the setInterval.

  • 1

    This will lock the browser, need a setInterval as said Felipe.

  • Yes, I’m sorry, I forgot to mention that. Anyway, anyone who can test, do a jsfiddle and give feedback to see if it solves the boy’s problem =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.