8f5553306c2cf7f4b14153f6117f8e9b

Hello. I'd like to fire a javascript event 1000 milliseconds after an event, but I'd like to CANCEL that event if there's activity again. So a simple setTimeout wont work because it'll fire regardless. I have the following but it kinda works and kinda doesn't.

showMoveControls is triggered on mouseover and hideMoveControls is triggered on mouseout.

As you can probably tell, I'm showing additional menu options if a user hovers over a toolbar for a few moments. But if they casually are just mousing up to the toolbar or over it without pausing, then I don't want those options to show.

Any help is appreciated.

var killFlag=null;
function showMoveControls() {
     killFlag=null;
     setTimeout("doOpenMoveControls()", 1000)
}
function doOpenMoveControls() {
     if (!killFlag) {
         $('move_controls').show();
    }
}
function hideMoveControls() {
     killFlag=1;
     $('move_controls').hide();
}

Refactorings

No refactoring yet !

F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

April 30, 2009, April 30, 2009 19:26, permalink

1 rating. Login to rate!

This is not 100% ideal but should work for your situation. you should generalize functionality like this as much as you can, just helps to remove any state tied to it, because you will want to use this again most likely :)

(function($){
  $.fn.delayedBind = function(type, duration, callback) {
    return this.bind(type, function(){
      var self = this
      setTimeout(function(){
        callback.call(self)
      }, duration)
    })
  }
})(jQuery)


// change the value of any input to 'Wahoo' after hovering for 1 second

$(function(){ 
  $('input').delayedBind('mouseover', 1000, function(){
    $(this).attr('value', 'Wahoo')
  })
})
Fbb264f64b4ce69bae04764280141faf

openid.aol.com/forwardfootmedia

May 7, 2009, May 07, 2009 23:26, permalink

No rating. Login to rate!

you may find this article on state machines of interest... good read either way. http://autonomousmachine.com/2008/12/30/list-ordering-with-a-javascript-state-machine

8f5553306c2cf7f4b14153f6117f8e9b

Danny Peck

May 11, 2009, May 11, 2009 14:54, permalink

No rating. Login to rate!

thx for the article and other help guys.

Your refactoring





Format Copy from initial code

or Cancel