Created
October 30, 2009 13:55
-
-
Save subtleGradient/222350 to your computer and use it in GitHub Desktop.
delayUnless — fire an event after a delay unless it's cancelled by another event; DEMO: http://mooshell.net/JvwnU/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // -*- Mode: MooTools Javascript; tab-width: 4; -*- | |
| /* | |
| --- | |
| provides: delayUnless | |
| author: Thomas Aylott <subtlegradient.com> | |
| copyright: 2009 Thomas Aylott | |
| license: MIT Style | |
| requires: [MooTools, Element, Events] | |
| ... | |
| */ | |
| function delayUnless(eventName1,delay,eventName2){ | |
| var eventKey = eventName1 + ':delay-unless('+delay+','+eventName2+')'; | |
| var timerKey = eventKey + '::timer'; | |
| if (!delayUnless.cache) delayUnless.cache = {}; | |
| if (delayUnless.cache[eventKey]) return eventKey; | |
| else delayUnless.cache[eventKey] = true; | |
| function start(event){ | |
| cancel.apply(this); | |
| this.store(timerKey, actuallyHappen.delay(delay,this,[event]) ) | |
| }; | |
| function cancel(){ | |
| $clear(this.retrieve(timerKey)); | |
| }; | |
| function actuallyHappen(event){ | |
| event.event = { | |
| stop:$empty, | |
| stopPropagation:$empty, | |
| preventDefault:$empty, | |
| returnValue:true | |
| }; | |
| this.fireEvent(eventKey,event); | |
| }; | |
| function add(){ | |
| remove.apply(this); | |
| this | |
| .addEvent(eventName1, start) | |
| .addEvent(eventName2, cancel) | |
| ; | |
| }; | |
| function remove(){ | |
| this | |
| .removeEvent(eventName1, start) | |
| .removeEvent(eventName2, cancel) | |
| ; | |
| }; | |
| Element.Events[eventKey] = { | |
| onAdd: add, | |
| onRemove: remove | |
| }; | |
| return eventKey; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <style> | |
| h4 { | |
| padding:1em; | |
| margin-bottom:1em; | |
| } | |
| </style> | |
| <h3>mouseenter</h3> | |
| <p>mouseleave before 1sec to cancel the event</p> | |
| <h4 id="foo">foo</h4> | |
| <h3>mouseenter</h3> | |
| <p>enter back before 1sec to cancel the event</p> | |
| <h4 id="bar">bar</h4> | |
| <h3>mousedown</h3> | |
| <p>hold down to fire second event</p> | |
| <h4 id="baz">baz</h4> | |
| <h3>keyup</h3> | |
| <p>keep hitting keys to stop the second event from firing</p> | |
| <input id="shmoo">shmoo</input> | |
| <p id="shmoo2"></p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // User Code | |
| window.addEvent('domready',function(){ | |
| $('foo') | |
| .addEvent(delayUnless('mouseenter',1000,'mouseleave'), function(){ | |
| this.set('text',' mouseenter:delay-unless(1000,mouseleave)'); | |
| this.tween('background-color','#ff0'); | |
| }) | |
| .addEvent('mouseenter', function(){ | |
| this.set('text',' mouseenter'); | |
| this.tween('background-color','#f00'); | |
| }) | |
| ; | |
| $('bar') | |
| .addEvent(delayUnless('mouseleave',1000,'mouseenter'), function(){ | |
| this.set('text',' mouseleave:delay-unless(1000,mouseenter)'); | |
| this.tween('background-color','#0ff'); | |
| }) | |
| .addEvent('mouseleave', function(){ | |
| this.set('text',' mouseleave'); | |
| this.tween('background-color','#00f'); | |
| }) | |
| ; | |
| $('baz') | |
| .addEvent(delayUnless('mousedown',1000,'mouseup'), function(){ | |
| this.set('text',' mousedown:delay-unless(1000,mouseup)'); | |
| this.tween('background-color','#0ff'); | |
| }) | |
| .addEvent('mousedown', function(){ | |
| this.set('text',' mousedown'); | |
| this.tween('background-color','#00f'); | |
| }) | |
| ; | |
| $('shmoo') | |
| .addEvent(delayUnless('keyup',1000,'keydown'), function(){ | |
| $('shmoo2') | |
| .set('text',' keyup:delay-unless(1000,keydown)') | |
| .tween('background-color','#0ff') | |
| ; | |
| }) | |
| .addEvent('keyup', function(){ | |
| $('shmoo2') | |
| .set('text',' keyup') | |
| .tween('background-color','#00f') | |
| ; | |
| }) | |
| ; | |
| }); | |
| |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment