代码之家  ›  专栏  ›  技术社区  ›  nibot

JavaScript超时了吗?

  •  17
  • nibot  · 技术社区  · 16 年前

    这个 window.setTimeout

    id = setTimeout(function, delay);
    

    其中“delay”是您希望调用函数的未来毫秒数。在此时间过去之前,您可以使用以下方法取消计时器:

    clearTimeout(id);
    

    我想要什么 是 早一点 或

    如果有getTimeout方法,您可以执行以下操作:

    originally_scheduled_time = getTimeout(id);
    updateTimeout(id, originally_schedule_time + new_delay);  // change the time
    

    是否有办法访问计划报警列表并修改它们?

    谢谢

    5 回复  |  直到 16 年前
        1
  •  14
  •   gnarf    16 年前

    如果你真的想要这种功能,你需要自己编写。

    function setAdvancedTimer(f, delay) {
      var obj = {
        firetime: delay + (+new Date()), // the extra + turns the date into an int 
        called: false,
        canceled: false,
        callback: f
      }; 
      // this function will set obj.called, and then call the function whenever
      // the timeout eventually fires.
      var callfunc = function() { obj.called = true; f(); }; 
      // calling .extend(1000) will add 1000ms to the time and reset the timeout.
      // also, calling .extend(-1000) will remove 1000ms, setting timer to 0ms if needed
      obj.extend = function(ms) {
        // break early if it already fired
        if (obj.called || obj.canceled) return false; 
        // clear old timer, calculate new timer
        clearTimeout(obj.timeout);
        obj.firetime += ms;
        var newDelay = obj.firetime - new Date(); // figure out new ms
        if (newDelay < 0) newDelay = 0;
        obj.timeout = setTimeout(callfunc, newDelay);
        return obj;
      };
      // Cancel the timer...  
      obj.cancel = function() {
        obj.canceled = true;
        clearTimeout(obj.timeout);
      };
      // call the initial timer...
      obj.timeout = setTimeout(callfunc, delay);
      // return our object with the helper functions....
      return obj;
    }
    
    var d = +new Date();
    var timer = setAdvancedTimer(function() { alert('test'+ (+new Date() - d)); }, 1000);
    
    timer.extend(1000);
    // should alert about 2000ms later
    
        2
  •  4
  •   jensgram    16 年前

    我相信没有。更好的方法可能是编写自己的包装器来存储计时器(func-ref、delay和timestamp)。这样,您可以通过清除计时器来假装更新计时器,并计算一个具有更新延迟的副本。

        3
  •  2
  •   James    16 年前

    另一个包装器:

    function SpecialTimeout(fn, ms) {
        this.ms = ms;
        this.fn = fn;
        this.timer = null;
        this.init();
    }
    
    SpecialTimeout.prototype.init = function() {
        this.cancel();
        this.timer = setTimeout(this.fn, this.ms);
        return this;
    };
    
    SpecialTimeout.prototype.change = function(ms) {
        this.ms += ms;
        this.init();
        return this;
    };
    
    SpecialTimeout.prototype.cancel = function() {
        if ( this.timer !== null ) {
            clearTimeout(this.timer);
            this.timer = null;
        }
        return this;
    };
    

    用法:

    var myTimer = new SpecialTimeout(function(){/*...*/}, 10000);
    
    myTimer.change(-5000); // Retard by five seconds
    myTimer.change(5000); // Extend by five seconds
    myTimer.cancel(); // Cancel
    myTimer.init(); // Restart
    
    myTimer.change(1000).init(); // Chain!
    
        4
  •  0
  •   Igor Zinov'yev    16 年前

    这可能不是你想要的,但看看吧,也许你可以利用它为你带来好处。

    解决方案是一个名为 getDelayedHandlers . 例如,您有一个显示和隐藏菜单的函数

    function handleMenu(show) {
        if (show) {
            // This part shows the menu
        } else {
            // This part hides the menu
        }
    }
    

    然后,您可以通过以下方式为其创建延迟处理程序:

    var handlers = handleMenu.getDelayedHandlers({
        in: 200, // 'in' timeout
        out: 300, // 'out' timeout
    });
    

    handlers

    var element = $('menu_element');
    element.observe('mouseover', handlers.over);
    element.observe('mouseout', handlers.out);
    

    要使此解决方案起作用,您需要使用 curry Prototype

        5
  •  -2
  •   Sarfraz    16 年前

    一种可能是这样的:

    if (this condition true)
    {
      setTimeout(function, 5000);
    }
    elseif (this condition true)
    {
      setTimeout(function, 10000);
    }
    else
    {
      setTimeout(function, 1000);
    }
    

    这取决于你如何构造你的条件或逻辑。谢谢