How do I write a blocking synchronous method in Javascript?

blocking, javascript, synchronous

Solution

How about a loop that checks time?

function sleep(milliSeconds){
    var startTime = new Date().getTime();                    // get the current time
    while (new Date().getTime() < startTime + milliSeconds); // hog cpu until time's up
}

Problem

I'm trying to mock out a method which takes a long time for testing purposes but can't find a good way to do this in Javascript. Any good approaches besides writing a very long for loop?

Original source