1. function mySetInterval(callback, interval) {
    2. var timer = null;
    3. var now = Date.now;
    4. var startTime = now();
    5. var endTime = startTime;
    6. var loop = function() {
    7. timer = requestAnimationFrame(loop); // 使用requestAnimationFrame
    8. endTime = now();
    9. if(endTime - startTime >=interval) {
    10. startTime = endTime = now();
    11. callback && callback(timer);
    12. }
    13. }
    14. timer = requestAnimationFrame(loop);
    15. return timer;
    16. }
    17. var count = 0;
    18. mySetInterval(function(timer){
    19. console.log('超级定时器已部署');
    20. count++;
    21. if(count>=3) {
    22. cancelAnimationFrame(timer);
    23. }
    24. }, 500)