I'm trying to simulate an async callback, that does something in a set number of seconds. I want these to all log at the same time, 3 seconds from when they are triggered. Right now they log consecutively 3 seconds after each other. The sleep functions are blocking the whole script from running. Any idea why?
function sleep(delay) {
var start = new Date().getTime();
while (new Date().getTime() < start + delay);
}
var same = function(string, callback) {
new sleep(3000);
return callback(string);
}
same("same1", function(string) {
console.log(string);
});
same("same2", function(string) {
console.log(string);
});
same("same3", function(string) {
console.log(string);
});