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?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
If you can use the newer await/await syntax:
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:
The code for
sleep.php
on the server side:Now you can create blocking synchronous functions in JavaScript (with the exception of Firefox) as follows:
How about a loop that checks time?