search

Home  >  Q&A  >  body text

node.js - nodejs如何定时不停的向某个文件写入

nodejs如何定时不停的向某个文件写入,比如每隔2s向log.txt.文件写入一段文本,写入的代码如下:


想实现一个后台不停的写入log,在网页循环的展示log.txt的内容。

ringa_leeringa_lee2788 days ago418

reply all(4)I'll reply

  • 怪我咯

    怪我咯2017-04-17 16:21:20

    Loop: infinite loop setTimeout
    Recursive: setTimeout recursive
    setInterval timing

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 16:21:20

        setInterval(()=>{
            /**
             * 文件操作逻辑
             */
        },2000)

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 16:21:20

    If you want to complete this loop operation without accessing the nodejs program through the command line or browser, you can try node-schedule, a scheduled execution module of nodejs, host the project in the background using pm2, and then use the scheduled module Just execute the logic code once in 2S

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 16:21:20

    https://github.com/merencia/n...
    node 版 crontab

    reply
    0
  • Cancelreply