L
L
Ler Den2018-12-03 20:40:56
JavaScript
Ler Den, 2018-12-03 20:40:56

"Eternal" process setInterval vs setTimeout?

The task is to periodically receive information from the remote api on the server with a certain time interval. The process itself will start with the server:

http.createServer(app).listen(PORT, function () {
   runForrestRun();
 });

function runForrestRun() {
    shared.timeoutId = setTimeout(
        function () {
            console.log("Hello");
        },
        shared.timeout);
}

At the same time, you need to be able to stop this process, start it again or change the time interval through api, for example like this (I did not check the performance of the first two endpoints)
const shared = require('./shared');
const handler = require('./handler');

// start process
router.post('/api/start', function (request, response) {
    runForrestRun();
    handler.success(response, 'ok');
});

// stop process
router.post('/api/stop', function (request, response) {
    clearTimeout(shared.timeoutId);
    handler.success(response, 'ok');
});

// update timeout interval
router.post('/api/timeout', function (request, response) {
    shared.timeout = request.body.timeout;
    handler.success(response, 'ok');
});

// get current timeout interval
router.get('/api/timeout', function (request, response) {
    handler.success(response, '', shared.timeout);
});

Another similar task is to notify users about something by mail or on the site (although this particular case is not about mail) Which is better setInterval or setTimeout in such cases? Or are there other better technologies out there?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Ler Den, 2018-12-04
@givemoneybiatch

The question is not fully resolved, but for now I stopped there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question