gpt4 book ai didi

javascript - 如何在 javascript 中依次激活 3 个不同的函数?

转载 作者:行者123 更新时间:2023-11-28 03:32:54 26 4
gpt4 key购买 nike

我已经尝试了一段时间了,但我无法让它工作。我的问题:我有 3 个功能(1 个缩小 div,1 个更新 div,1 个放大 div),通过鼠标单击激活。但它们同时激活,这意味着第一个(使 div 变小)由于第二个(更新 div)而被跳过。第二个函数调用一个 php 函数来重新加载 div 中的数据。以下是 3 个函数:

function reduceView(Parent){
$(Parent).find('#details').hide("slow", function(){
// Animation complete.
});
// get width and Extend
$(Parent).find("#" + Parent.ID + "").width("250px");
var width = $(Parent).find("#" + Parent.ID + "").css("width");
$(Parent).animate({
width: width
}, 500, function(){
// Animation complete.
});
}

function renewWindow(Parent){
$(":animated").promise().done(function(){
PHP.execute(Parent.id + "/home");
$(Parent).find("div[class='Item-list']").remove();
$(Parent).find("div[id='details']").remove();
$(Parent).find("div[id='confirmDialog']").remove();
$(Parent).append(PHP.response);
initJquery();
initEvents();
});
}

function enlargeView(Parent){
$(Parent).promise().done(function(){
$(Parent).find('#details').show("slow", function(){
// Animation complete.
});
// get width and Extend
$(Parent).find("#" + Parent.ID + "").width("683px");
var width = $(Parent).find("#" + Parent.ID + "").css("width");
$(Parent).animate({
width: width
}, 500, function(){
// Animation complete.
});
});
}

任何线索如何让这 3 个按顺序工作?'initJquery' 和 'initEvents' 确保 .click 等已重新初始化。提前致谢!!

附言。如果我遗漏了您可能需要的任何信息,请随时询问!!

编辑:下面是调用函数的代码

$(".selecter").click(function() {
var Element = this;
var ID = Element.id;
var Parent = $(Element).closest(".drag-div")[0];
reduceView(Parent);
renewWindow(Parent);

$(":animated").promise().done(function(){
PHP.execute(Parent.id + "/details?" + Parent.id + "id="+ID);
$(Parent).find('#details').html(PHP.response);
enlargeView(Parent);
initJquery();
});
$(Element).addClass("selected");
});

最佳答案

看起来您正在应用多个动画,然后使用 $(":animated") 选择器获得您想要的动画的 promise 。您的答案中没有足够的代码来运行它,但我的猜测是 renewWindow 中的闭包是由错误的 promise 对象触发的 - 可能是页面上的一个甚至与此功能无关的对象。

尝试从第一个函数中返回您想要关闭的 promise 对象,如下所示:

function reduceView(Parent){
var promise = $(Parent).find('#details').hide("slow", function(){
// Animation complete.
}).promise();
// get width and Extend
$(Parent).find("#" + Parent.ID + "").width("250px");
var width = $(Parent).find("#" + Parent.ID + "").css("width");
$(Parent).animate({
width: width
}, 500, function(){
// Animation complete.
});
return promise;
}

然后,renewWindow 可以使用该 promise :

function renewWindow(Parent, promise){
promise.done(function(){
PHP.execute(Parent.id + "/home");
$(Parent).find("div[class='Item-list']").remove();
$(Parent).find("div[id='details']").remove();
$(Parent).find("div[id='confirmDialog']").remove();
$(Parent).append(PHP.response);
initJquery();
initEvents();
});
}

这样你就知道你用来触发函数的 promise 就是你用来隐藏 div 的 promise。

关于javascript - 如何在 javascript 中依次激活 3 个不同的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16484001/

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