How can i put a loop in this server

前端 未结 3 1204
臣服心动
臣服心动 2021-01-25 01:15

For example, something similar i want to do:

var http = require(\'http\');

const PORT=8080;
var myvar = \"Test: \";

function handleRequest(request, response){
         


        
3条回答
  •  攒了一身酷
    2021-01-25 01:37

    If you just want to continuously process data, you can use the setInterval function, which will continuously call a function with a small interval between. This will give the rest of your code time to run.

    // synchronous code, with a while loop
    while(true) {
      // do something
    }
    // NOTE: anything after this never runs
    
    // asynchronous code, with setInterval
    setInterval(function() { 
      // do something
    }, 10);
    

    10 here is the number of milliseconds between each time the code is run. For most purposes (such as continuously reading for a pin), a small number (such as 10 milliseconds, or 100 times per second) should work fine.

提交回复
热议问题