Y
Y
Yarik2020-04-22 16:49:38
JavaScript
Yarik, 2020-04-22 16:49:38

How to update timer time?

When the page is refreshed, a timer is fired, if the user clicks on the button, then the timer time needs to be updated to the initial

let t = 1000*5;
$('html').on('click', '.ApiOnline', function () {
        t = 1000*5;
})

setTimeout(function () {
  $('.status-on').css({'display':"none"})
}, t);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yariik, 2020-04-22
@Yariik

Remade by myself

class dynamicTimer {

    constructor(func, delay) {
      this.callback = func
      this.triggerTime = delay
      this.timer = 0
      this.updateTimer()
    }

    updateTimer() {
      clearTimeout(this.timer)
      let delay = this.triggerTime
      // console.log("Current delay: ", delay)
      this.timer = setTimeout(this.callback, delay)
      return this
    }

    addTime(delay) {
      this.triggerTime = delay
      this.updateTimer()
      return this
    }
  }

  let timer = new dynamicTimer(function() {
    $('.status-on').css({'display':"none"})
  }, 3000)

  $('html').on('click', '.ApiOnline', function () {
    timer.addTime(3000)
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question