How do I schedule a function one time in a future?


  • GNU/Linux

    I need to schedule a function one time in a a certain future. I use this:

    var timeout=setTimeout(function(){
    
    			function();
    			console.log("UNO");
    		}, 10000);
    		 clearTimeout(timeout);
    

    But I read in the console the value "UNO" immediatly, and not after 10000 ms. Anyone can help me and suggest me how can I resolve this problem?


  • Admin

    You don't need clearTimeout. Just do this

    setTimeout(function () {
        callSomeOtherFunction();
        console.log('UNO');
    }, 10000);
    

  • GNU/Linux

    @baris Does it exist some method with node that I can use to schedule a function in a future. For example, my code is not a good solution because, If I must restart server , I lose the timeout. What do you suggest?


  • Admin

    If you need to handle server restarts you would have to save some data in the database, like the timestamp you want the function to be executed, so after a restart you can check that value and run the function.


  • Admin

    You may also want to consider using cron.


Log in to reply
 


Looks like your connection to NodeBB was lost, please wait while we try to reconnect.