Created
April 8, 2015 09:21
-
-
Save supershabam/86185da9e3433c5502d4 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/gujatabahu
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/rxjs/2.3.22/rx.all.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
var fn = function() { | |
console.log(new Date().toString()); | |
return new Promise(function(resolve, reject) { | |
var timeout = Math.random() * 2000 + 500; | |
console.log('running for', timeout); | |
setTimeout(function() { | |
resolve('done'); | |
}, timeout); | |
}); | |
}; | |
// ticker will run at most one fn at a time attempting | |
// to run it on a consistent schedule set by the splay | |
// and period. If the function is still executing when | |
// a scheduled run occurs, then the function is not | |
// called. | |
function ticker(splay, period, fn) { | |
var isRunning = false; | |
return Rx.Observable.timer(splay, period). | |
filter(function() { | |
return !isRunning; | |
}). | |
flatMap(function() { | |
isRunning = true; | |
var p = fn(); | |
p.then(function() { | |
isRunning = false; | |
}); | |
return p; | |
}); | |
} | |
ticker(500, 2000, fn).subscribe(function(x) { | |
console.log(x); | |
}); | |
</script> | |
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html> | |
<html> | |
<head> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/rxjs/2.3.22/rx.all.js"><\/script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
</body> | |
</html></script> | |
<script id="jsbin-source-javascript" type="text/javascript">var fn = function() { | |
console.log(new Date().toString()); | |
return new Promise(function(resolve, reject) { | |
var timeout = Math.random() * 2000 + 500; | |
console.log('running for', timeout); | |
setTimeout(function() { | |
resolve('done'); | |
}, timeout); | |
}); | |
}; | |
// ticker will run at most one fn at a time attempting | |
// to run it on a consistent schedule set by the splay | |
// and period. If the function is still executing when | |
// a scheduled run occurs, then the function is not | |
// called. | |
function ticker(splay, period, fn) { | |
var isRunning = false; | |
return Rx.Observable.timer(splay, period). | |
filter(function() { | |
return !isRunning; | |
}). | |
flatMap(function() { | |
isRunning = true; | |
var p = fn(); | |
p.then(function() { | |
isRunning = false; | |
}); | |
return p; | |
}); | |
} | |
ticker(500, 2000, fn).subscribe(function(x) { | |
console.log(x); | |
}); | |
</script></body> | |
</html> |
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
var fn = function() { | |
console.log(new Date().toString()); | |
return new Promise(function(resolve, reject) { | |
var timeout = Math.random() * 2000 + 500; | |
console.log('running for', timeout); | |
setTimeout(function() { | |
resolve('done'); | |
}, timeout); | |
}); | |
}; | |
// ticker will run at most one fn at a time attempting | |
// to run it on a consistent schedule set by the splay | |
// and period. If the function is still executing when | |
// a scheduled run occurs, then the function is not | |
// called. | |
function ticker(splay, period, fn) { | |
var isRunning = false; | |
return Rx.Observable.timer(splay, period). | |
filter(function() { | |
return !isRunning; | |
}). | |
flatMap(function() { | |
isRunning = true; | |
var p = fn(); | |
p.then(function() { | |
isRunning = false; | |
}); | |
return p; | |
}); | |
} | |
ticker(500, 2000, fn).subscribe(function(x) { | |
console.log(x); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment