0

I have interval that needs to do some http requests after one minute

   setInterval(() => {
      this.takeTableDataCallInterval();
    }, 60000);

How can i get counter minus from my Interval Number ?

For example every one second i want to show something like

60 seconds Left until new call is maked 59 seconds Left until new call is maked

When it comes to 0 it should start over

Petar
  • 119
  • 1
  • 8
  • Change 60000 to 1000ms so it runs every second – Steve Tomlin Jun 03 '21 at 12:28
  • You need to start by making the interval duration one second, otherwise you won’t be able to update anything every second in the first place. – CBroe Jun 03 '21 at 12:29
  • Also create a counter, and increase it each time, and once it reaches 60, you can clear the interval. Take a look at this example https://stackoverflow.com/a/2956980/1471485 – John Jun 03 '21 at 12:30
  • @SteveTomlin I can't make intervaal on every second i need to update my table rows on every one minute – Petar Jun 03 '21 at 12:34
  • @Petar - "For example every one second i want to show something like" – Steve Tomlin Jun 03 '21 at 13:15

3 Answers3

3

Here is an example:

it works every one second in order to do the countdown but once it reach to the 60 it resets the countdown and does the request.

const timer = document.querySelector('#timer');
let counter = 60;

setInterval(() => {
  if (counter === 0) {
    //this.takeTableDataCallInterval();
    counter = 60;
  }
  else {
    timer.innerHTML = `${counter} seconds left`;
    counter--;
  }
}, 1000);
<p id="timer"></p>
InspectorGadget
  • 879
  • 6
  • 20
1

Counting shorter setInterval periods is one way of doing this, although timings here are rarely accurate enough to decently measure the passage of time, and you'll see drift (i.e. 60 one second intervals won't sum to exactly 60s)

Instead, record a "due time" with a high accuracy using performance.now()

let dueTimeMs = performance.now() + 60000;

then set an interval with relatively short time

setInterval(() => {
    const nowMs = performance.now();
    const timeUntilDueMs = dueTimeMs - nowMs;
    // report remaining time
    if(timeUntilDueMs <= 0){
        // do something
        dueTimeMs += 60000; 
    }
},100);
spender
  • 117,338
  • 33
  • 229
  • 351
0

Try this:

let timeout;

function resetTimeout(limit) {
    timeout = limit;
}

function serviceCall() {
    console.log('Service call made')
}

setInterval(() => {
    if(timeout == 0 || timeout == null) {
        (timeout == 0) ? serviceCall() : ''
        resetTimeout(60);
    }
    console.log(`${timeout}s left until new call is made`)
    timeout --;
}, 1000);
Saranga B
  • 1,635
  • 1
  • 3
  • 9
  • Dear Prometheus, welcome to the community. Thank you for your answer. To help the person asking better it is always nice to explain your code a bit better. – Kound Jun 03 '21 at 19:03
  • @Kound sure. :) So here we declare a timeout variable along with two methods resetTimeout and serviceCall. The resetTimeout method is used to initialize the timeout during the first iteration of the interval, and in subsequent iterations it will be reset. The service call is only made when the timeout variable is zero. The timeout variable is decremented in every iteration. – Saranga B Jun 04 '21 at 04:05