たとえばポーリングなど何かしらの条件が満たされるまで定期的にチェックし続けたいような処理をきれいにかけないか試してみた例。
limit
はチェック回数だけど、タイムアウトでもいいかもしれない。
function execution(func, interval, limit) {
var id, count;
setInterval(function () {
count++;
if(func.call() || (!isNaN(limit) && limit === count)) {
clearInterval(id);
}
}, interval);
}
execution(function() {
if(Math.random() > 0.5) return false; //条件に満たない
// Do something.
return true;
}, 100);