// Log a message (line) on the screen.
function log(message) {
$('#log').append('<br>' + message);
}
function prependZero(anInt) {
if (anInt>=0 && anInt < 10) {
return '0' + anInt;
}
return anInt.toString();
}
function getCurrentTime() {
var dt = new Date();
var hours = prependZero(dt.getHours());
var minutes = prependZero(dt.getMinutes());
var seconds = prependZero(dt.getSeconds());
var millis = prependZero(dt.getMilliseconds());
var time = hours + ":" + minutes + ":" + seconds + "." + millis;
return time;
}
// The most generic function that only waits and returns a promise.
function wait(t, v) {
var d = new $.Deferred();
setTimeout(function() {
d.resolve(v);
}, t);
return d.promise();
}
// Logs the value and returns the next number.
function markStep(nr) {
log('step ' + nr + ': ' + getCurrentTime());
return nr+1;
}
// Waits before logging and returns a promise for the next number.
function takeStep(nr) {
return wait(stepTime, nr).then(markStep);
}
var stepTime = 1000;
log('start (' + getCurrentTime() + ')');
takeStep(0)
.then(takeStep)
.then(takeStep)
.then(takeStep)
.then(takeStep)
.done(function(nr) {
log('done (' + getCurrentTime() + ')');
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.