<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>用 setTimeout 模拟 setInterval</title> </head> <body> <script> function myInterval(func, delay) { function inside(a) { func(a); setTimeout(inside, delay, 5); } setTimeout(inside, delay, 5); } myInterval(like, 1000); function like(a) { console.log(a); } function newInterval(func, delay) { function insede(a) { func(a); setTimeout(inside, delay, 5); } setTimeout(inside, delay, 5); } newInterval(like,1000,5); function like(5){ console.log(5); } </script> </body> </html>