Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中生成同步函数?_Javascript_Jquery_Synchronous - Fatal编程技术网

在javascript中生成同步函数?

在javascript中生成同步函数?,javascript,jquery,synchronous,Javascript,Jquery,Synchronous,我想要同步函数,就像jQuery的$.ajax({..,async:false,..}) ,这些都包括一些效果,如fadeIn、Out、slide。。。等等 但是我发现如果这些函数调用如下 A(); B(); C(); 所有效果几乎同时开始。在我的理解中,这是因为函数是同步调用的,但这并不意味着函数B()是在函数A()完全完成后启动的。。对吧? 那么,我怎样才能使这些函数有序地工作呢 我找到了一种使用回调函数的方法,但这对我来说还不够。我想每个函数都会发出一些ajax请求或动画 functio

我想要同步函数,就像jQuery的
$.ajax({..,async:false,..})

,这些都包括一些效果,如fadeIn、Out、slide。。。等等

但是我发现如果这些函数调用如下

A();
B();
C();
所有效果几乎同时开始。在我的理解中,这是因为函数是同步调用的,但这并不意味着函数B()是在函数A()完全完成后启动的。。对吧?

那么,我怎样才能使这些函数有序地工作呢


我找到了一种使用回调函数的方法,但这对我来说还不够。

我想每个函数都会发出一些ajax请求或动画

function A() {
  return $.ajax(....);
  // or
  // return $('#animate').animate({ width: 100 }, 1000 ).promise();
  // or
  // var def = $.Defered();
  // in async function def.resolve();
  // return def.promise();
}

function B() {
  return $.ajax(....); 
  // or 
  // return $('#animate').animate({ width: 200 }, 3000 ).promise();
  // var def = $.Defered();
  // in async function def.resolve();
  // return def.promise();
}

function C() {
  return $.ajax(....);
  // or
  // return $('#animate').animate({ width: 300 }, 1000 ).promise();
  // var def = $.Defered();
  // in async function def.resolve();
  // return def.promise();
}

$.when(A(), B(), C(), function (aRes, bRes, cRes) {
})

//C().then(B()).then(A()).done(function () {
//  console.log('DONE');  
//});
要获得更详细的答案,请解释您的函数的作用

这可以让您按顺序运行每个函数,一个接一个地等待。例如:

var a = function() {
    var defer = $.Deferred();

    console.log('a() called');

    setTimeout(function() {
        defer.resolve(); // When this fires, the code in a().then(/..../); is executed.
    }, 5000);

    return defer;
};

var b = function() {
    var defer = $.Deferred();

    console.log('b() called');

    setTimeout(function () {
        defer.resolve();
    }, 5000);

    return defer;
};

var c = function() {
    var defer = $.Deferred();

    console.log('c() called');

    setTimeout(function () {
        defer.resolve();
    }, 5000);

    return defer;
};

a().then(b).then(c);

使用
defer.resolve()
意味着您可以控制该函数何时让下一个函数执行。

您确实指定了三个同步函数,这意味着B将仅在A完成时触发

但是,执行异步任务(如启动动画)不会阻止A完成,因此B似乎不会等待完成

了解jQuery延迟。延迟是一种设计模式,因此它并不特定于jQuery,但是它们有一个很好的实现

function methodA () {
    var dfd = $.Deferred();

    console.log('Start A');

    // Perform async action, like an animation or AJAX call
    $('#el').slideOut(2000, function () {
        // This callback executes when animation is done.
        console.log('End A');
        dfd.resolve();
    });

    // Return unchangeable version of deferred.
    return dfd.promise();
};

function methodB () {
    console.log('Start B');
};

methodA().then(methodB);

这些函数的内部是什么?在jquery animate(如果您正在使用)中,您唯一可以使用的是回调函数来执行下一个动画,除非函数显式异步,否则所有函数都会逐个执行。A()、B()和C()都执行一些异步操作吗?请向更改我的帖子的编辑器发送-“链接总是很好:)”在我第一次尝试时,我确实将链接放在了deferred上,但文本编辑器标记为“不允许链接到jQuery之类的东西。”所以我当时就把它省略了。我也喜欢链接:)SOF实际上隐藏了最后一行,它真正将这一切结合在一起。a(0)。然后(b)。然后(c)这个评论的存在是为了帮助下一个没有注意到左边滚动条的人:D
function methodA () {
    var dfd = $.Deferred();

    console.log('Start A');

    // Perform async action, like an animation or AJAX call
    $('#el').slideOut(2000, function () {
        // This callback executes when animation is done.
        console.log('End A');
        dfd.resolve();
    });

    // Return unchangeable version of deferred.
    return dfd.promise();
};

function methodB () {
    console.log('Start B');
};

methodA().then(methodB);