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
You could make a synchronous AJAX call to a server which defers the response by a certain amount of time as requested by your script. Note however that this method won't work in Firefox as it doesn't support synchronous AJAX calls.
Just use this simple function on the client side:
function sleep(microseconds) {
var request = new XMLHttpRequest;
request.open("GET", "/sleep.php?time=" + milliseconds);
request.send();
}
The code for sleep.php on the server side:
usleep(intval($_GET("sleep")));
Now you can create blocking synchronous functions in JavaScript (with the exception of Firefox) as follows:
alert("Hello");
sleep(1000000); // sleep for 1 second
alert("World");