SOURCE

// function mySetTimeout(callback, delay) {
//     // 记录开始时间
//     const startTime = performance.now();

//     // 使用循环检查是否到达指定时间
//     function check() {
//         const elapsed = performance.now() - startTime;
//         if (elapsed >= delay) {
//             callback();
//         } else {
//             // 如果还没到时间,继续检查
//             // 使用requestAnimationFrame或setTimeout来避免阻塞主线程
//             if (typeof requestAnimationFrame !== 'undefined') {
//                 requestAnimationFrame(check);
//             } else {
//                 // 如果没有requestAnimationFrame,使用setTimeout递归
//                 setTimeout(check, Math.max(0, delay - elapsed));
//             }
//         }
//     }

//     // 开始检查
//     check();
// }

// mySetTimeout(() => {
//     console.log(111111)
// }, 1000)

// const timerMap = {};
// let id = 0;

// function mySetTimeout(callback, delay, ...args) {
//     const timerId = id++;

//     const startTime = performance.now();

//     function check() {
//         const elapsed = performance.now() - startTime;
//         if (elapsed >= delay) {
//             delete timerMap[timerId];
//             callback.apply(null, args);
//         } else {
//             timerMap[timerId] = requestAnimationFrame(check);
//         }
//     }

//     timerMap[timerId] = requestAnimationFrame(check);
//     return timerId;
// }

// function myClearTimeout(timerId) {
//     if (timerMap[timerId]) {
//         cancelAnimationFrame(timerMap[timerId]);
//         delete timerMap[timerId];
//     }
// }

// // 使用自定义setTimeout
// const timerId = mySetTimeout(() => {
//     console.log('执行回调');
// }, 1000);

// // 如果需要取消
// // myClearTimeout(timerId);


function* timerGenerator(callback, delay) {
    const start = Date.now();
    console.log(Date.now() - start)
    while (Date.now() - start < delay) {
        yield Date.now() - start;
    }
    callback();
}

function genSetTimeout(callback, delay) {
    const timer = timerGenerator(callback, delay);

    function run() {
        console.log(timer.next())
        if (!timer.next().done) {
            requestAnimationFrame(run);
        }
    }

    run();
}

// 使用示例
genSetTimeout(() => console.log('Generator 实现的 setTimeout'), 1000);



// function channelSetTimeout(callback, delay) {
//   const channel = new MessageChannel();
//   channel.port2.postMessage('');
//   channel.port1.onmessage = () => {
//     const start = Date.now();
//     function check() {
//       if (Date.now() - start >= delay) {
//         callback();
//       } else {
//         requestAnimationFrame(check);
//       }
//     }
//     check();
//   };
// }

// // 使用示例
// channelSetTimeout(() => console.log('MessageChannel 实现的 setTimeout'), 1000);



// function abortSetTimeout(callback, delay) {
//     const controller = new AbortController();
//     const signal = controller.signal;

//     const start = Date.now();

//     function check() {
//         if (signal.aborted) return;

//         if (Date.now() - start >= delay) {
//             callback();
//         } else {
//             requestAnimationFrame(check);
//         }
//     }

//     requestAnimationFrame(check);

//     return {
//         clear: () => controller.abort()
//     };
// }

// // 使用示例
// const timer = abortSetTimeout(() => console.log('AbortController 实现的 setTimeout'), 1000);
// // timer.clear(); // 取消定时器


// function workerSetTimeout(callback, delay) {
//     const workerCode = `
//     self.onmessage = function(e) {
//       const start = performance.now();
//       console.log(performance.now() - start)
//       while (performance.now() - start < e.data) {}
//       self.postMessage('');
//     };
//   `;

//     const blob = new Blob([workerCode], { type: 'application/javascript' });
//     const worker = new Worker(URL.createObjectURL(blob));

//     worker.onmessage = () => {
//         callback();
//         worker.terminate();
//     };

//     worker.postMessage(delay);

//     return {
//         clear: () => worker.terminate()
//     };
// }

// // 使用示例
// workerSetTimeout(() => console.log('Web Worker 实现的 setTimeout'), 1000);
console 命令行工具 X clear

                    
>
console