0

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!

4
  • 2
    this is going to start a million intervals, over and over and over. Pretty sure that's not what you want, so before you show your code, explain what it is you actually wanted to achieve, then show the code, then explain how you think it was supposed to achieve what you wanted. Commented Jul 5, 2021 at 20:09
  • the "middle part" should be setTimout instead of an interval, seems like the answer to your question for me Commented Jul 5, 2021 at 20:14
  • @john can you please edit the code with setTimeout. I already tried that but had the same output. Commented Jul 5, 2021 at 20:16
  • You know ... setInterval doesn't execute exactly at 500ms, but both setInterval and setTimeout is executed after 500ms. So if you check twice per second, that means the value will sometimes not be checked for a second, and you will "miss out" on that value change. Instead, I suggestion to set a variable and then send out an event that executes your code. Commented Jul 5, 2021 at 20:26

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.