How to add dynamic promises or delay the current?

My main goal is to postpone the counter until it starts.

Imagine a timeout disables 'n' to '0' and then fires an event, but it can be reset asynchronously to "n" every time something happens.

I tried to achieve this using 'promises' with $ q.

I tried to add dynamically new promises with a timeout in the new delay chain.

But before reaching this goal, I could not even knit new promises.

Example:

var d1 = $q.defer(); var d2 = $q.defer(); d1.promise.then(function(data) { return 'd1' + data; }); d2.promise.then(function(data) { return 'd2' + data; }); var t = $q.all([d1.promise]); t.then(function(data) { console.log(data); }); $timeout(function() { d1.resolve("1000"); }, 1000); $timeout(function() { t.then(function() { d2.resolve("800"); }); }, 800); 

It only outputs: ["1000"] instead of ["1000", "800"]

My search:

stack overflow.squite

angular-promise-tracker I can’t understand this code, how it behaves exactly for what I need. But this is done for something else.

+4
source share
1 answer

If you want to just use your own javascript then this should work:

 // create a handle to the timeout var toHandle = window.setTimeout(X); // put this in the event that causes a reset window.clearTimeout(toHandle); toHandle = window.setTimeout(X); 

Where X is the function that you want to execute when delayed. It also gives you the ability to change the delay value as you wish.

Greetings.

+1
source

Source: https://habr.com/ru/post/1496171/


All Articles