0

how to make the function work only after click button for one second?

eg

$("#button").click(function(){
$(".panel").addClass('flip');

I want 'flip' only if the button is pressed for 1 second. if you click on it for less than a second it does not 'flip'

Please suggest.

LGVentura
  • 1,547
  • 1
  • 11
  • 17

1 Answers1

2
$(document).ready(function(){
  $("#button").mousedown(function(e){
    var self = this;
    clearTimeout(self.mouseUpTimer);
    self.mouseUpTimer = setTimeout(function(){
      $(self).bind('mouseup.delay', function(e){
        $(".panel").addClass('flip');
        $(self).unbind('mouseup.delay');
      });
    }, 1000);
  }).mouseup(function(e){
    clearTimeout(this.mouseUpTimer);
  });
});

jsFiddle: http://jsfiddle.net/zSPhK/

Kyle
  • 21,978
  • 2
  • 60
  • 61
  • thank you very much. Now, how do the reverse? pressing for more than 1 second, it does not 'flip', only if you press for less than 1 second – LGVentura Jul 18 '12 at 13:00
  • It should work for anything greater than or equal to a 1 second mousedown event. – Kyle Jul 18 '12 at 13:07
  • Thanks, but not exactly what I need. This should not work in the "MouseDown" but the "MouseUp". Then, the event should occur after the MouseUp. when MouseUp, if pressed (mousedown) for 1 second (hold) it will 'flip'. when you MouseUp, if pressed (mousedown) for less than one second (click) does not 'flip'. suggest? – LGVentura Jul 18 '12 at 13:50
  • I edited the answer and provided an example. Please update your question to reflect your needs. – Kyle Jul 18 '12 at 14:39
  • http://stackoverflow.com/questions/11544948/click-and-hold-to-scroll-touchstart-touchmove-touchend – LGVentura Jul 18 '12 at 16:25
  • Dude, you answered correctly all I asked in this topic, so I'll leave it as is because it can help the needs of others. However, while experimenting, I discovered that not meet my needs. so I opened another topic which best explain all the settings that I need. If you can help me, I'll be grateful. Thank you. – LGVentura Jul 18 '12 at 16:26
  • Incidentally, this code is used for any longer than 1 second. As can use it to values ​​less than or equal to one second? – LGVentura Jul 18 '12 at 16:39