* Runs the provided callback after the specified delay. This uses a micro * task for 0 or no specified time. This means that the delay will actually * be close to 0 and this will NOT yield to the event queue. * * Returns the timer ID that can be used to cancel the timer (cancel method).
(callback, opt_delay)
| 51 | * @return {number|string} |
| 52 | */ |
| 53 | delay(callback, opt_delay) { |
| 54 | if (!opt_delay) { |
| 55 | // For a delay of zero, schedule a promise based micro task since |
| 56 | // they are predictably fast. |
| 57 | const id = 'p' + this.taskCount_++; |
| 58 | this.resolved_ |
| 59 | .then(() => { |
| 60 | if (this.canceled_[id]) { |
| 61 | delete this.canceled_[id]; |
| 62 | return; |
| 63 | } |
| 64 | callback(); |
| 65 | }) |
| 66 | .catch(reportError); |
| 67 | return id; |
| 68 | } |
| 69 | const wrapped = () => { |
| 70 | try { |
| 71 | callback(); |
| 72 | } catch (e) { |
| 73 | reportError(e); |
| 74 | throw e; |
| 75 | } |
| 76 | }; |
| 77 | const index = this.win.setTimeout(wrapped, opt_delay); |
| 78 | if (getMode().test) { |
| 79 | if (!timersForTesting) { |
| 80 | timersForTesting = []; |
| 81 | } |
| 82 | timersForTesting.push(index); |
| 83 | } |
| 84 | return index; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Cancels the previously scheduled callback. |
no test coverage detected