• xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!


    JavaScript 如何使用 setTimeout 实现 setInterval

    website multi content page

    setIntervalSimulator

    "use strict";
    
    /**
     *
     * @author xgqfrms
     * @license MIT
     * @copyright xgqfrms
     * @created 2020-07-27
     * @modified
     *
     * @description setInterval
     * @difficulty Easy Medium Hard
     * @complexity O(n)
     * @augments
     * @example
     * @link https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval
     * @solutions
     *
     */
    
    const log = console.log;
    
    const setIntervalSimulator = (callback, time, count = 10) => {
      function interval(callback, time) {
        const begin = new Date().getTime();
        const timerID = setTimeout(() => {
          clearTimeout(timerID);
          const end = new Date().getTime();
          log(`time =`, (end - begin) / 1000);
          // 排除 callback 执行时间的干扰
          callback();
          // 同步执行
          if(count) {
            log(`count =`, count);
            count--;
            interval(callback, time);
          }
        }, time);
      }
      // init
      interval(callback, time);
      // requestAnimationFrame();
    }
    
    // setIntervalSimulator(() => console.log(`OK`), 1000 * 10);
    // setIntervalSimulator(() => console.log(`OK`), 1000 * 3);
    // setIntervalSimulator(() => console.log(`OK`), 10);
    setIntervalSimulator(() => console.log(`OK 1000`), 1000);
    // setIntervalSimulator(() => console.log(`OK 0`), 0);
    
    
    
    // time = 1.005
    // OK 1000
    // count = 10
    // time = 1.002
    // OK 1000
    // count = 9
    // time = 1.005
    // OK 1000
    // count = 8
    // time = 1.003
    // OK 1000
    // count = 7
    // time = 1.005
    // OK 1000
    // count = 6
    // time = 1.002
    // OK 1000
    // count = 5
    // time = 1.004
    // OK 1000
    // count = 4
    // time = 1.004
    // OK 1000
    // count = 3
    // time = 1.005
    // OK 1000
    // count = 2
    // time = 1.005
    // OK 1000
    // count = 1
    // time = 1.003
    // OK 1000
    
    
    

    setTimeoutSimulator

    
    "use strict";
    
    /**
     *
     * @author xgqfrms
     * @license MIT
     * @copyright xgqfrms
     * @created 2020-07-27
     * @modified
     *
     * @description setTimeout
     * @difficulty Easy Medium Hard
     * @complexity O(n)
     * @augments
     * @example
     * @link https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout
     * @solutions
     *
     */
    
    const log = console.log;
    
    const setTimeoutSimulator = (callback, time) => {
      const begin = new Date().getTime();
      // const begin = performance.now();
      // ReferenceError: performance is not defined
      const timerID = setInterval(() => {
        clearInterval(timerID);
        const end = new Date().getTime();
        // const end = performance.now();
        log(`time =`, (end - begin) / 1000);
        // 排除 callback 执行时间的干扰
        callback();
      }, time);
    }
    
    // setTimeoutSimulator(() => console.log(`OK`), 1000 * 10);
    // setTimeoutSimulator(() => console.log(`OK`), 1000 * 3);
    // setTimeoutSimulator(() => console.log(`OK`), 10);
    setTimeoutSimulator(() => console.log(`OK 1000`), 1000);
    setTimeoutSimulator(() => console.log(`OK 0`), 0);
    
    
    // OK 0
    // time = 0.006
    // OK 1000
    // time = 1.003
    
    // OK 0
    // time = 0.006
    // OK 1000
    // time = 1.006
    
    // OK 0
    // time = 0.007
    // OK 1000
    // time = 1.002
    
    
    

    js 函数返回值, timoutID

    https://www.cnblogs.com/xgqfrms/p/13388644.html

    blank page

    打印的是什么? event order id ?

    Symbol

    Symbol 实现原理, uuid

    
     key1 = Symbol(`key`);
    // Symbol(key)
     key2 = Symbol(`key`);
    // Symbol(key)
    
     key1 == key2;
    // false
     key1 === key2;
    // false
    
    

    refs

    https://javascript.info/settimeout-setinterval

    https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval

    https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout


    Flag Counter

    ©xgqfrms 2012-2020

    www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


  • 相关阅读:
    如何让nodejs使用多线程执行
    web input光标的颜色
    web视频自定义规划
    webgl 学习注意事项
    前端url创建,以及base64与url的转化
    微信 input 照相机 呼出
    js 资源下载方法
    在React项目中使用React-intl实现多语言支持,以及对react-init各组件的解读
    React Native 的组件定义及使用
    AMD模块&CommonJs模块&ES6模块
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/13376334.html
Copyright © 2020-2023  润新知