NodeJS中使用事件监听器定时器和回调

博客介绍了在NodeJs中利用时间间隔执行定期工作(setInterval)和用超时时间延迟工作(setTimerout)的相关内容,并提及了执行结果。

一。用时间间隔执行定期工作 setInterval

      /**
         * To schedule the repeated execution of <i><code>callback</code></i> every <i><code>delay</code></i> milliseconds. <p>
         * Returns a <i><code>intervalObject</code></i> for possible use with <i><code>clearInterval()</code></i>. Optionally you can also pass arguments to the callback.
         *
         * @param {Function} callback
         * @param {number} delay
         * @param {...*} args
         * @return {Object}
         */
        function setInterval(callback, delay, args) {
        }
        
        
        var x = 0, y = 0, z = 0;
        function dipalyValues() {
            console.log("X=%d;Y=%d;Z=%d", x, y, z);
        }
        function updateX() {
            x+=1;
        }
        function updateY() {
            y +=1;
        }
        function updateZ() {
            z+=1;
            dipalyValues();
        }
        setInterval(updateX, 500);
        setInterval(updateY, 1000);
        setInterval(updateZ, 2000);

结果

结果

二。用超时时间来延迟工作 setTimerout

    /**
     * To schedule execution of a one-time <i><code>callback</code></i> after <i><code>delay</code></i> milliseconds.<p>
     * Returns a <i><code>timeoutObject</code></i> for possible use with <i><code>clearTimeout()</code></i>. Optionally you can also pass arguments to the callback.
     * <p>
     * It is important to note that your callback will probably not be called in exactly <i><code>delay</code></i> milliseconds - Node.js makes no guarantees about the exact timing of when the callback will fire, nor of the ordering things will fire in. The callback will be called as close as possible to the time specified.
     * @param {Function} callback
     * @param {number} delay
     * @param {...*} args
     * @return {Object}
     */
    function setTimeout(callback, delay, args) {
    }
    
    function simpelTimeOut(consoleTimer) {
        console.timeEnd(consoleTimer);
    }
    console.time("twoSecond");
    setTimeout(simpelTimeOut, 2000, "twoSecond");
    console.time("oneSecond");
    setTimeout(simpelTimeOut, 1000, "oneSecond");
    console.time("fiveSecond");
    setTimeout(simpelTimeOut, 5000, "fiveSecond");
    console.time("50MinlliSecond");
    setTimeout(simpelTimeOut, 50, "50MinlliSecond");

结果

结果这里插入图片描述

JavaScriptNode.js都采用事件循环机制来处理异步操作。事件循环是一种执行模型,它允许JavaScript在单线程上运行,同时处理异步操作。下面是JavaScriptNode.js事件循环的简要介绍: JavaScript事件循环: JavaScript事件循环由一个事件队列一个执行栈组成。当代码执行时,它会被添加到执行栈中。如果代码中包含异步操作,例如定时器或者事件监听器,那么这些操作会被添加到事件队列中。当执行栈为空时,事件队列中的下一个事件会被取出并添加到执行栈中执行。这个过程会一直重复,直到事件队列为空。 Node.js事件循环: Node.js事件循环与JavaScript事件循环类似,但是它包含了更多的阶段。Node.js事件循环包含了6个阶段,分别是timers、I/O callbacks、idle、prepare、pollcheck。在每个阶段中,Node.js会执行相应的操作。例如,在timers阶段中,Node.js会执行所有的定时器回调函数。在poll阶段中,Node.js会等待I/O事件完成并执行相应的回调函数。如果事件队列为空并且没有被设置任何定时器,那么Node.js会在这个阶段等待新的事件被添加到事件队列中。 下面是一个简单的Node.js事件循环的例子: ```javascript // 输出结果:timer1 timer2 promise1 promise2 console.log('timer1'); setTimeout(() => { console.log('timer2'); }, 0); Promise.resolve().then(() => { console.log('promise1'); }).then(() => { console.log('promise2'); }); console.log('end'); ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值