【问题标题】:Where does JQuery call setInterval for .fadeIn and .fadeOut()?JQuery 在哪里为 .fadeIn 和 .fadeOut() 调用 setInterval?
【发布时间】:2012-03-17 08:39:31
【问题描述】:

这是一个 source viewer 用于 fadeIn() 但由于某种原因它没有链接到 custom()。

另外,作为第二个问题,如何将所有这些代码概念化...有这么多函数调用,我什至无法猜测调用 .fadeIn() 或 .fadeOut 时实际运行了多少代码()。

例如:

fadeIn() 调用animate() 调用speed() 调用extend()...

作为第三个问题:这是面向对象的编程吗?

谢谢。

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    它不使用setTimeout(),它使用setInterval(),在custom()中,在animate()方法的底部调用。

    这里有一个很好的教程:http://www.schillmania.com/content/projects/javascript-animation-1/

    如教程中所述,setTimeout() 安排事件在调用setTimeout() 函数后的一定时间发生。问题是调度函数运行需要时间,因此下一次超时将在第一次超时的延迟和执行代码所用的时间之后调度。

    如果你愿意:

    100ms : function x called 
    200ms : function x called 
    300ms : function x called 
    400ms : function x called
    

    你做到了:

    function x(){
        setTimeout( x , 100);
    }
    setTimeout( x , 100);
    

    接下来会发生什么:

    100ms : function x called
    first calls execution time + 200ms : function x called
    second calls execution time + first calls execution time + 300ms : function x called
    third calls execution time + second calls execution time + first calls execution time + 400ms : function x called
    

    所以你这样做:

    function x(){
    
    }
    setInterval( x , 100);
    

    【讨论】:

      【解决方案2】:

      别搞错了,这个面向对象的编程。函数式编程完全不同(通常看起来也很不同,搜索pythonhaskell)。我认为您的意思是“命令式编程”(例如 C),它不是函数式编程,但也没有对象。

      jQuery 也支持继承,类似于 JavaScript 的原生原型继承。

      【讨论】:

        【解决方案3】:

        setInterval 通常是一个不好的选择。这是因为一旦设置了间隔,它将尝试每 N 毫秒执行一次,而不管现有线程如何运行 - 因此,如果您的浏览器已经因一些严重问题而窒息,那么 setInterval 将添加到其中。对于使用 setInterval,您必须仔细考虑,尤其是在较旧的、较少线程感知的浏览器中。更多信息在这里:https://stackoverflow.com/a/5479821/1238884

        setTimeout 通常会更好,因为您只在流程完成时排队下一个超时 - 有时这样的事情会更好:

        function foo(params) {
          // do stuff here that might block or take time
          setTimeout(function() {
              foo(params);
          }, 1234);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-11-20
          • 1970-01-01
          • 1970-01-01
          • 2013-11-28
          • 1970-01-01
          • 2011-12-13
          • 2011-10-01
          相关资源
          最近更新 更多