settimeout

Test a function that contains a setTimeout()

北城以北 提交于 2021-02-05 20:18:31
问题 I have a close function in my component that contains a setTimeout() in order to give time for the animation to complete. public close() { this.animate = "inactive" setTimeout(() => { this.show = false }, 250) } this.show is bound to an ngIf . this.animate is bound to an animation. I have a test that needs to test this function it("tests the exit button click", () => { comp.close() fixture.detectChanges() //verifies the element is no longer in the DOM const popUpWindow = fixture.debugElement

Test a function that contains a setTimeout()

谁说我不能喝 提交于 2021-02-05 20:12:10
问题 I have a close function in my component that contains a setTimeout() in order to give time for the animation to complete. public close() { this.animate = "inactive" setTimeout(() => { this.show = false }, 250) } this.show is bound to an ngIf . this.animate is bound to an animation. I have a test that needs to test this function it("tests the exit button click", () => { comp.close() fixture.detectChanges() //verifies the element is no longer in the DOM const popUpWindow = fixture.debugElement

How exactly are the function calls ordered in an asynchronous JavaScript program?

▼魔方 西西 提交于 2021-02-05 07:49:05
问题 I am learning the concept of asynchronous programming in JavaScript (JS). But, I am having a hard time understanding the same. For the last few days, I had been reading various articles on the internet to understand it, but I am unable to grasp the idea. So, here are the doubts I have: setTimeout(function(){ alert("Hello 1"); }, 3000); // .....(i) console.log("Hi!"); // .....(ii) setTimeout(function(){ alert("Hello 2"); }, 2000); // .....(iii) Consider the above code. I learnt that JS uses a

why does a function with setTimeout not lead to a stack overflow

人盡茶涼 提交于 2021-02-04 16:45:38
问题 I was writing a test for handling huge amounts of data. To my surprise, if I added a setTimeout to my function, it would no longer lead to a stack overflow (how appropriate for this site). How is this possible, the code seems to be really recursive. Is every setTimeout call creating it's own stack? Is there way to achieve this behavior (handle a huge array/number asynchronous and in order) without increasing the needed memory? function loop( left: number, callbackFunction: (callback: () =>

How to control setTimeout with promises

旧巷老猫 提交于 2021-01-29 11:39:38
问题 var functionsArray = [ function() { setTimeout(function() { console.log(1); }, 100); }, function() { setTimeout(function() { console.log(2); }, 200); }, function() { setTimeout(function() { console.log(3); }, 10); } ], Say I have an array of functions like above(the number of functions in it is not known). I want to write a function which takes this array as parameter and executes them in sequence. In the example above, I want it to log 1,2,3 in the sequence. Since Promise.all does not

How to avoid the explicit Promise construction antipattern for this concurrent timer that should reset when the promise completes?

倖福魔咒の 提交于 2021-01-28 18:20:29
问题 I have written a pretty simply script: { waiting: false, async handleWaiting(promise, timeout) { return new Promise((res, rej) => { let loadingStarted = false; const timeoutInstance = setTimeout(() => { loadingStarted = true; this.waiting = true; }, timeout); const onFinished = () => { if (loadingStarted) { this.waiting = false; } clearTimeout(timeoutInstance); } promise .then((result) => { onFinished(); res(result); }) .catch((ex) => { onFinished(); rej(ex); }); }); }, async searchForTerm

Poll API until a path in the response object is Successful | Failure - Typescript

眉间皱痕 提交于 2021-01-28 11:48:31
问题 I have a function which accepts a string and a path value and checks whether the path at the result returns a 1 or a -1. I fire the function with multiple requests and everything seems to be successful except for one. For example, if I call the function with 10 different URL's continously (one by one, not in an array), the promise is resolved for 9 but not for the 10th one. This is my code: enum Status { Queued = 0, Started = 1, Finished = 2, Failed = -1, } let dataFetchingTimer: number;

Poll API until a path in the response object is Successful | Failure - Typescript

微笑、不失礼 提交于 2021-01-28 11:41:32
问题 I have a function which accepts a string and a path value and checks whether the path at the result returns a 1 or a -1. I fire the function with multiple requests and everything seems to be successful except for one. For example, if I call the function with 10 different URL's continously (one by one, not in an array), the promise is resolved for 9 but not for the 10th one. This is my code: enum Status { Queued = 0, Started = 1, Finished = 2, Failed = -1, } let dataFetchingTimer: number;

Simple way to synchronously execute code after setTimout() code is done

烈酒焚心 提交于 2021-01-27 21:38:50
问题 I need a simple way to wait for setTimeout code to finish executing and then run the code that comes after setTimeout. Now the code after loop containing setTimout is executing before loop/setTimout is finished executing. for(let i = 0; i < 5; i++) { setTimeout(function(){ console.log(i); }, i*1000); } console.log("loop/timeout is done executing"); 回答1: setTimeout is by definition not synchronous - whatever you use to solve the issue will have to be asynchronous, there's no way around that.

Why is clearTimeout not clearing the timeout in this react component?

我只是一个虾纸丫 提交于 2021-01-27 05:40:14
问题 I am attempting to clear a former timeout before initiating a new timeout, because I want messages to display for 4 seconds and disappear UNLESS a new message pops up before the 4 seconds is up. The Problem: Old timeouts are clearing the current message, so clearTimeout() is not working in this component, in this scenario: let t; // "t" for "timer" const [message, updateMessage] = useState('This message is to appear for 4 seconds. Unless a new message replaces it.'); function clearLogger() {