0

In my nodejs app, i have a file with following contents:

index.js contents:

var app = require('express')();
var http = require('http').Server(app);

function start(){
    var timer = setTimeout(function(){
        check();
        clearTimeout(timer);
    }, 60000); // 1 minutes
}

function check(){
    // my custom codes....
    console.log('checked');
    start(); //return again to start
}

start();

http.listen(3008, function(){
  console.log('listening on *:' + 3008);
});

Up code most check every 60 seconds my check function. this code is work, but there is a problem, when for example 5 users online in my app, i see in my console the check function after 60 seconds was repeated !

//First 60 seconds
checked

//Second 60 seconds
checked
checked

//third 60 seconds
checked
checked
checked
checked

// check function repated again in each 60 seconds !

i try test several way, but not work.

Booshveg
  • 1
  • 1
  • i use this file as an api for my site. this function check my users payment every 60 seconds, but the request was duble in next 60 sec. – Booshveg May 05 '20 at 13:56
  • In face, i need somthing to check a function in each 60 seconds – Booshveg May 05 '20 at 14:48
  • and i know, no body can help me to fix this solution. Do you know what my superiority is over others? With my own efforts, I can accurately, to the extent permitted, and pursue it. I don't remember ever leaving any project unfinished. And that's why I always have my customers. Something you can rarely see. I will become stronger every day. – Booshveg May 05 '20 at 14:52
  • it's because your timer reference is not bound to the closure it is created in, when used in your setTimeout function: https://stackoverflow.com/questions/36209784/variable-inside-settimeout-says-it-is-undefined-but-when-outside-it-is-defined – developer May 05 '20 at 14:56
  • Thanks friend, i test this way and not work in my case. thanks so, I never remember it being something and I couldn't make it. I would love to be something I can't make, but I'll do it anyway, and the standard of quality is my priority. Anyway i will try to fix. – Booshveg May 05 '20 at 15:02
  • And one thing I forgot to say. I have never worked for money, I have always worked out of curiosity. Maybe that's the key to my success over the rest. However, I know my income is higher than others. I'm never looking to be famous. Read my words, it will help you along the way.Goodbye – Booshveg May 05 '20 at 15:12

1 Answers1

0

Have you tried setInterval(function, timeout);

var app = require('express')();
var http = require('http').Server(app);

function start(){
    var timer = setInterval(function(){
        check();
    }, 60000); // 1 minutes
}

function check(){
    // my custom codes....
    console.log('checked');
}

start();

http.listen(3008, function(){
    console.log('listening on *:' + 3008);
});
amitk7407
  • 1
  • 2