ygssoni
ygssoni

Reputation: 7349

Nested setInterval javascript

I have a JS code like this

function Entry(){
  var Trigger = setInterval(function () {
      var ent = EntryZone(); //EntryZone is a function which returns a value that change in every second
      if (Value <= ent) { // Value is a variable
        clearInterval(Trigger); 
        var TakeEntry = setInterval(function () {
          var chkPer = Percentage(); //Percentage() is a function which returns a value that change in every second
            if (Value >= chkPer) { //Here I am comparing Value with chkPer
              var interval = setInterval(function(){    
                if (Value >= chkPer){ //Here I want to compare Vlaue again with chkPer after 10 seconds. If it has still the same or grater value then proceeding further.
                  BuyNow();
                  clearInterval(TakeEntry);
                }
                clearInterval(interval);
              },10000);
            }
        }, 500);
      }
  }, 500);
}

Ok, so that is what I am trying to do. But as I run the script its messing up. The Middle part where I want to check the value again after 10 seconds is executing multiple time. I just want to execute it once. And if the value is not same after 10 seconds then clear the "interval" interval and go back and continue "TakeEntry" interval. Please guide me what is wrong with the code. Thank you!

Upvotes: 0

Views: 203

Answers (2)

trincot
trincot

Reputation: 350147

The problem is that the call to setInterval( ....., 10000) is not stopping the other interval to keep on ticking...

This kind of logic is much easier to implement with async await syntax:

// Utility function to await a given delay:
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));

async function entry() {
    while (true) { // Keep doing?
        var ent = EntryZone();
        while (Value > ent) {
            await delay(500);
            ent = EntryZone();
        }
        while (true) {
            var chkPer = Percentage();
            if (Value >= chkPer) {
                await delay(10000);
                chkPer = Percentage();
                if (Value >= chkPer) {
                    BuyNow();
                    break;
                }
            }
            await delay(500);
        }
    }
}

Upvotes: 1

edemaine
edemaine

Reputation: 3120

I think this sort of code is a lot easier to write correctly with setTimeout instead of setInterval. Here's what I believe you meant from your description, but you could easily modify the code to e.g. trigger Entry again in certain situations. The idea is that, at any moment, at most one of the three functions should have a timeout scheduled.

function Entry(){
  setTimeout(function () {
    var ent = EntryZone(); //EntryZone is a function which returns a value that change in every second
    if (Value <= ent) { // Value is a variable
      TakeEntry();
    } else {
      Entry();
    }
  }, 500);
}
function TakeEntry(){
  setTimeout(function () {
    var chkPer = Percentage(); //Percentage() is a function which returns a value that change in every second
    if (Value >= chkPer) { //Here I am comparing Value with chkPer
      Check10();
    } else {
      TakeEntry();
    }
  }, 500);
}
function Check10(){
  setTimeout(function(){    
    if (Value >= chkPer){ //Here I want to compare Value again with chkPer after 10 seconds. If it has still the same or grater value then proceeding further.
      BuyNow();
    } else {
      TakeEntry();
    }
  }, 10000);
}

Upvotes: 1

Related Questions