JavaScript - Is it possible to view all currently scheduled timeouts?

前端 未结 4 787
一整个雨季
一整个雨季 2020-12-08 19:25

So I was wondering, is there any feasible way in JavaScript to view information about scheduled timeouts and intervals that you don\'t explicitly know about (I know se

相关标签:
4条回答
  • 2020-12-08 19:34

    You could also create a timer manager module which will keep track of current timers and allow you to get, add, stop and stop all timers.

    var timers = (function() {
      //
      var timers = []
    
      //
      const getIndex = (array, attr, value) => {
          for (let i = 0; i < array.length; i += 1) {
              if (array[i][attr] === value) {
                  return i
              }
          }
          return -1
      };
    
      // add
      const add = (callback, time) => {
        var id = setTimeout(() => {
          let index = getIndex(timers, 'id', id)
          timers.splice(index, 1)
          callback()
        }, time)
        timers.push({
          id: id,
          time: time,
          debug: callback.toString()
        })
      };
    
      // get all active timers
      const all = () => timers
    
      // stop timer by timer id
      const stop = (id) => {
        if (!isNaN(id)) {
          let index = getIndex(timers, 'id', id)
          if (index !== -1) {
            clearTimeout(timers[index].id)
            timers.splice(index, 1)
          }
        }
      };
    
      // stop all timers
      const stopAll = () => {
        for (let i = 0; i < timers.length; i++) {
          clearTimeout(timers[i].id)
        }
        timers = []
      };
    
      return {
        add: add,
        all: all,
        stop: stop,
        stopAll: stopAll,
      };
    })();
    
    //
    timers.add(function() {
      console.log("timeout 1 fired");
    }, 1000)
    
    timers.add(function() {
      console.log("timeout 2 wont get fired");
    }, 2000)
    
    timers.add(function() {
      console.log("timeout 3 fired");
    }, 3000)
    
    timers.add(function() {
      console.log("timeout 4 fired, timers", timers.all());
    }, 4000)
    
    timers.add(function() {
      console.log("timeout 5 fired");
    }, 5000)
    
    console.log('All timers', timers.all())
    
    console.log("kill timer 2")
    timers.stop(2)

    Run the snippet to see it in action.

    0 讨论(0)
  • 2020-12-08 19:35

    We've just published a package solving this exact issue.

    npm install time-events-manager
    

    With that, you can view them via timeoutCollection & intervalCollection objects.

    0 讨论(0)
  • 2020-12-08 19:50

    No, even the HTML5 spec (which is a rationalisation of the HTML 4.01 behaviour in current browsers, with additional features) doesn't specify a way to list the available callbacks.

    0 讨论(0)
  • 2020-12-08 19:51

    how about simply rewriting the setTimeout function to sort of inject custom logging functionality?

    like

    var oldTimeout = setTimeout;
    window.setTimeout = function(callback, timeout) {
      console.log("timeout started");
      return oldTimeout(function() {
        console.log('timeout finished');
        callback();
      }, timeout);
    }
    

    might work?

    0 讨论(0)
提交回复
热议问题