【问题标题】:jQuery build a queue with custom functions (not talking about fx queues)jQuery 使用自定义函数构建队列(不讨论 fx 队列)
【发布时间】:2009-08-21 09:56:40
【问题描述】:

这是我的代码...

var _before = function () {
    for (var i = 0; i <= 10000; i++) {
        console.log(i);
    }
};

var _appear = function () {
    console.log('i am in the middle of it');
};

var _after = function () {
    console.log('all done!');
};

jQuery.fn.doSomething = function() {
    this.click(function() {
        _before();
        _appear();
        _after();
    });
};

$('#someButton').doSomething();

我想在 doSomething 函数中构建一个队列,以允许 _before() 在触发 _appear() 之前完成,并让 _appear() 在触发 _after() 之前完成。

我无法更改代码的结构。

谢谢

【问题讨论】:

  • _before() 和 _appear() 到底在做什么,才能让 _appear() 在 _before() 完成之前开始执行?按照您现在编写的方式,它们是按顺序调用的,并且不涉及明显的异步调用。因此,_before() 将在 _appear() 被调用之前完成执行。
  • 这只是一个例子,我想建立一个队列......现在_appear在_before结束之前触发......

标签: jquery callback queue wait


【解决方案1】:

不明白为什么它不会是同步的,因为没有任何迹象表明它是异步的。

您是否尝试过将三个函数调用放在一个函数本身中:

jQuery.fn.doSomething = function() {
    this.click(function() {
        doAll();
    });
};

jQuery.fn.doAll = function() {
    _before();
    _appear();
    _after();
};

或者你可以不这样做,因为你不能改变结构?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 2015-07-28
    相关资源
    最近更新 更多