gpt4 book ai didi

javascript - jQuery $.when 延迟不工作

转载 作者:行者123 更新时间:2023-11-29 10:52:03 25 4
gpt4 key购买 nike

我想要做的是在通过 ajax 完成两个同时的图像加载时进行一些操作。为此,我创建了一个自定义 Deferred 以在图像加载完成时解析。

<div id="i"></div>
$(document).ready(function() {
$('#i').hide();
var imgLoad = loadImgs();
$.when(imgLoad).then(function() {
$('#i').show('slow');
});
});

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

var matrix = $.getJSON("https://graph.facebook.com/thematrixmovie");
var pulp = $.getJSON("https://graph.facebook.com/pulpfiction");

$.when(matrix, pulp).then(function(m, p) {
mImg = '<img src=' + m[0].picture + '>';
pImg = '<img src=' + p[0].picture + '>';
$('#i').show().append(mImg + pImg);
dfd.resolve;
});
return dfd.promise();
}

你可以试试这个on JSFiddle .

我用过 Eric Hynds post包括 working example作为引用,但仍然没有得到它的工作。有什么想法吗?

最佳答案

您正确地使用了 $.when,但是您尝试像这样调用 resolve 方法:

dfd.resolve;

与其他一些语言不同,JavaScript 不允许您在方法调用中省略括号。您只需添加它们,您的代码就可以正常工作!

dfd.resolve();

关于javascript - jQuery $.when 延迟不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8322707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com