/**
 * @since Last modified: 2024-01-29 10:14:05
 * @function delay
 * @description Delayed the code fragment, which can replace seTimeout.The ax.frame value will change and return to zero when done.
 * @param {object} options - Parameters of delay.
 * @param  {string} options.duration - Duration of delay.The unit is ms.
 * @param  {function} [options.doing] - The cb function in progress.
 * @param  {function} [options.done] - The cb function at the end.
 * @param  {string} [options.frame=0] - Incremental animation frames.
 * @returns  {Promise}  - The Promise.
 * @see {@link https://codepen.io/axui/pen/BaMqzbb|demo @ codepen}
 * @example
 * ax.delay({duration:2000,done:()=>{}});
 * <!--return Promise-->
 * ax.delay(...).then(()=>{console.log('done,resolve')},()=>{console.log('doing,reject')})
 */
'use strict';
const delay = function ({ duration = 2000, doing, done, frame = 0 }) {
    return new Promise((resolve, reject) => {
        try {
            if (!duration) {
                done && done(frame);
                resolve(frame);
            }
            let deadline = new Date().getTime() + duration, listen = () => {
                let newTime = new Date().getTime();
                if (newTime >= deadline) {
                    cancelAnimationFrame(frame);
                    done && done(frame);
                    resolve(frame);
                    frame = 0;
                }
                else {
                    frame = requestAnimationFrame(listen);
                    doing && doing(frame);
                }
            };
            listen();
        }
        catch (e) {
            reject(e);
        }
    });
};
export default delay;
