Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

setTimeout stop at 2:00 p.m. and 3:00 p.m

I would like to retrieve the value of BTC at 2 p.m. and 3 p.m. via a setTimeout.

How can I set the setTimeout? Am I required to use seconds?

function runTimers() {
  setTimeout(() => {
    ...

    setTimeout(() => {
      ...
    }, 1000);
  }, 2000);
}

So, the value 1000 = 1 seconde

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I convert 02:00 PM to minutes? So, the result is 840 minutes.

Then, I convert 840 minutes into secondes? So, 50400 secondes and I add 000 -> 50400000?

function runTimers() {
  setTimeout(() => {
    ...

    setTimeout(() => {
      ...
    }, 50400000);
  }, 54000000);
}

Do you agree with me? Is there a simpler solution?

function runTimers() {
  setTimeout(() => {
    let val1 = parseFloat(stockObject.p).toFixed(2);
    let price = parseFloat(stockObject.p).toFixed(2);

    stockPriceElementsec2.innerText = price;
    stockPriceElementsec2.style.color =
      !lastPrice || lastPrice === price
        ? 'black'
        : price > lastPrice
        ? '#AAFF00'
        : 'red';

    lastPrice = price;
    stockObject = null;

    setTimeout(() => {
      let val2 = parseFloat(stockObject.p).toFixed(2);
      let price = parseFloat(stockObject.p).toFixed(2);

      stockPriceElementsec3.innerText = price;
      stockPriceElementsec3.style.color =
        !lastPrice || lastPrice === price
          ? 'black'
          : price > lastPrice
          ? '#AAFF00'
          : 'red';

      lastPrice = price;
      stockObject = null;
    }, 1000);
  }, 2000);
}

Here is the complete code => Stackblitz.

>Solution :

setTimeout will just run some code after a given delay (in ms), so it is not the right solution if you want to check something at regular intervals.
What you’d rather want to use would probably be setInterval which works the exact same way, but the code fires up every X ms.
For instance, you could run this code every hour, and check that the current time is 2pm or 3pm.

let runTimers = setInterval(() => {
    let hours = new Date().getHours();
    if (hours < 14 || hours > 15) return; 
    let val1 = parseFloat(stockObject.p).toFixed(2);
    let price = parseFloat(stockObject.p).toFixed(2);

    stockPriceElementsec2.innerText = price;
    stockPriceElementsec2.style.color =
      !lastPrice || lastPrice === price
        ? 'black'
        : price > lastPrice
        ? '#AAFF00'
        : 'red';

    lastPrice = price;
    stockObject = null;
  }, 3600000);
}

this does the same thing, except it runs every hour. Let’s say you fire it up at 13:01 , it’ll show you the value at 14:01 and 15:01 and will show nothing at 16:01.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading