then()
返回一个 Promise
,当定时器完成时解析并执行回调。
then()
方法可以直接内联,像这样
createTimer({duration: 500}).then(callback);
或在 async
/ await
上下文中使用
async function waitForTimerToComplete() {
return createTimer({ duration: 250 })
}
const asyncTimer = await waitForTimerToComplete();
参数
名称 | 类型 |
---|---|
callback | 一个 Function ,其第一个参数返回定时器本身 |
返回
Promise
then() 代码示例
import { createTimer, utils } from 'animejs';
const [ $status ] = utils.$('.status');
const [ $time ] = utils.$('.time');
createTimer({
duration: 2000,
onUpdate: self => $time.innerHTML = self.currentTime,
})
.then(() => $status.innerHTML = 'fulfilled');
<div class="large row">
<div class="col">
<pre class="large log row">
<span class="label">promise status</span>
<span class="status value">pending</span>
</pre>
</div>
<div class="col">
<pre class="large log row">
<span class="label">current time</span>
<span class="time value lcd">0</span>
</pre>
</div>
</div>