Last active
December 30, 2015 06:09
-
-
Save coolwanglu/7786903 to your computer and use it in GitHub Desktop.
Hynopic Demos
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
/* | |
* Hypnotic can be extended | |
* Here we have a simple semaphore introduced | |
* which supports only one producer/consumer | |
*/ | |
// jQuery still works, of course! | |
function print(msg){ $('#output').html(msg); } | |
/* | |
* Hypnotic extensions must be run outside Hypnotic! | |
*/ | |
var semaphore_def = [' function SemaphoreOne() { };', | |
'SemaphoreOne.prototype = {', | |
' wait: new Hypnotic(function(cb) { this.cb = cb; }),', | |
' release: function() { if(this.cb) { setTimeout(this.cb, 1); this.cb = null; }}', | |
'}']; | |
window.eval(semaphore_def.join('')); | |
var sema = new SemaphoreOne(); | |
$(document).on('keypress', function(){ | |
sema.release(); | |
}); | |
print('Press any key to continue.'); | |
sema.wait(); // hmm... How can you do this without a sync sleep() ? | |
print('Got it!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment