Normally, the speed at which a function runs would make it impossible for a viewer to see the incremental changes in values or current positions in a traversal. However, this allowed me to solve my initial challenge of trying to visualize the progress of an algorithm solution to create a visualizer. Initially the uses for this action compared a "regular" timeout or interval may seem limited. In this example, the final operation of each iteration will not run until the promise from the sleep() function has been resolved. If we combine this with a setTimeout action, we can effectively create a sleep() function in Javascript. With the async/ await keywords in our functions, we can tell a function to "await" the resolution of a Promise before continuing its next action. This means that the state of the Promise will not be set to resolved until that delay time has passed. For the callback, we will use the resolve action of the Promise. For the delay, we simply pass the ms parameter of the sleep function. SetTimeout itself takes in two arguments: a callback function and a duration in milliseconds in which to delay invoking the callback. We do not need a callback for the Promise reject here, as this will never arise in this use case. We then create a new Promise object and use setTimeout in the callback function for the resolution of the Promise. In the example above, the function accepts a single parameter which will reflect the time in milliseconds that we would like the function to sleep for. The first argument is the function that will be invoked when the Promise is resolved, and the second (optional) is invoked if the Promise is rejected.Įnter fullscreen mode Exit fullscreen mode When creating a new Promise object, we can pass in two callback functions as arguments. For example, if you have ever worked with an API call and made a request to the server, that request returns a "pending" Promise that will ultimately become "resolved" or "rejected". In Javascript, a Promise object represents the eventual completion of an action. setTimeout allows us to wait a specified amount of time before invoking a new callback function with setInterval operating similarly except the delay will reset and continue repeating.įor the purposes of slowing down operation for something like a while loop, neither of these are directly helpful since they delay the invocation of a new action rather than the delay the original function in which they were called. For example, Ruby has sleep(1000) and Python has time.sleep(1) to "pause" operation for 1 second, but there is no direct correlate in Javascript.Īsynchronous actions in Javascript can usually call upon one of these two functions. Most program languages have a sleep function/method that can be invoked to delay the next operation in a function. How do I slow down the operation speed of a function to the point where I could create visual models and allow the viewer time to process them before it updated to the next step? I had planned to create a second entry in that series this week, however I ran into an obstacle: It accepts a function and a delay time in milliseconds.In my last blog entry, I had created a visualizer to model an approach to solving a Leetcode algorithm problem. The setTimeout() function schedules code for the runtime to execute once a set amount of time has passed. Using setTimeout() to Wait for a Specific Time
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |