首页 > 编程语言 >Node.js编写报时器,精度高、性能高

Node.js编写报时器,精度高、性能高

时间:2023-03-27 10:44:30浏览次数:35  
标签:报时器 Node interval printTime js delay nowDate time next

Node.js编写的报时器,精度高、性能高

// Function to print the current time every minute on the 0th second and with millisecond precision
function printTime(interval, oldDate) {
  // Get the current date and time
  const nowDate = new Date()
  const nowNextTime = Math.ceil(nowDate.getTime() / interval);
  const oldNextTime = Math.ceil(oldDate.getTime() / interval);
  // Calculate the delay until the next minute by subtracting the current time from the next minute and subtracting 1 second (1000 milliseconds) to account for the time it takes to print the time and schedule the next call
  const delay = (nowNextTime * interval - nowDate.getTime()) / 2;
  if (oldNextTime < nowNextTime) {
    console.log(nowDate)
  }
  // Schedule a call to the printTime function after the calculated delay
  setTimeout(() => {
    // Schedule the next call to the printTime function
    printTime(interval, nowDate);
  }, delay);
}

// Start the printing process by calling the printTime function for the first time
printTime(10000, new Date())

 

标签:报时器,Node,interval,printTime,js,delay,nowDate,time,next
From: https://www.cnblogs.com/xiangxisheng/p/17260740.html

相关文章

  • js - 稀释、防抖、节流 - 概念关系
    1.防抖期望:同一时间或者同一时间前后指定范围内【如1秒内】如果没有更多的操作,多次调一个函数时,希望只调一次函数实现:使用定时器,指定时间内更新定时器计时时长,直到为0......
  • js实现复制功能
    可以使用以下代码实现复制功能:1functioncopyText(){2varcopyText=document.getElementById("myText");3copyText.select();4document.execCommand("co......
  • JSON & import assertions All In One
    JSON&importassertionsAllInOneerror//constpackageInfo=require("./package.json");import*aspkgfrom"./package.json";console.log(`pkg`,pkg);......
  • scala的json项目
    有下面的scala代码packagejobsimportorg.json4s._importorg.json4s.jackson.JsonMethods._objectTest{defmain(args:Array[String]):Unit={case......
  • Linux环境下使用jsoncpp
    目录1.下载jsoncpp2.生成静态库libjsoncpp.a3.复制相关文件至/usr/local下(方便编程)4.CMakeList.txt编写(需要新增的)1.下载jsoncpp-->https://github.com/open-so......
  • go语言学习-json和xml
    JSONjson是完全独立于语言的文本格式,是k-v的形式name:zs应用场景:前后端交互,系统间数据交互json使用go语言内置的encoding/json标准库编码json使用json.Marshal()函数可以......
  • 初入了解——Vue.js
    目录 前言:一.Vue.js介绍二.主要功能三.语言特点易用灵活性能版本记录: Vue3.xVue2.x Vue1.0 Vue0.12 Vue0.11四.运行环境 前言:根据上篇文章来继续了解Vue一.V......
  • node.js 学习笔记
    阶段一1初始Node.jsjavascript运行环境1.2Node.js中的javacript运行环境1.3Node.js环境安装百度1.4node.js执行javaScript代码2fs文件系统模块2.1......
  • JS 做一个简单的 Parser
    前言前些天偶然看到以前写的一份代码,注意有一段尘封的代码,被我遗忘了。这段代码是一个简单的解析器,当时是为了解析日志而做的。最初解析日志时,我只是简单的正则加上分割,写......
  • centos7 yum -y install nodejs安装npm时报错,处理方法
     centos7yum-yinstallnodejs安装npm时报错,处理方法安装npm时报错,处理方法sudo:npm:commandnotfoundsudoyum-yinstallnodejsLoadedplugins:fastestmirr......