【问题标题】:how to wait until something executes in jquery then to execute another function如何等到在jquery中执行某些东西然后执行另一个函数
【发布时间】:2013-11-25 07:05:15
【问题描述】:

我有一个不显示的 div(“#ImageBingingDiv”),我将其淡入,然后更改它的 html 并添加图片,之后我尝试添加到标签 elevateZoom 但因为 div vas以某种方式显示无,它不会做应该做的事情,所以我认为我必须以某种方式等到创建图像标签才能添加 elevateZoom。有什么建议么?

$("#ImageBingingDiv").fadeIn(1000);
var widthD = parseInt($("#ImageBingingDiv").css('width')) - 10 + 'px';
var heightD = 0.5625 * (parseInt($("#ImageBingingDiv").css('width')) - 1);
$("#ImageBingingDiv").html('<img id="toZoom" data-zoom-image="./' + id + '" width="' + widthD + '" height="' + heightD + 'px" margin="10px" src="./' + id + '" alt=""/>');
$("#ImageBingingDiv").css('height', heightD + 12 + 'px').css('vertical-align', 'center');

$("#toZoom").elevateZoom({
    zoomWindowFadeIn: 500,
    zoomWindowFadeOut: 500,
    lensFadeIn: 500,
    lensFadeOut: 500
});

【问题讨论】:

  • 您是否尝试过使用下面提到的 settimeout?
  • 是的,我发现为什么它在 chrome 中由于某种原因没有执行,放大后它可以工作......现在我想知道为什么
  • 你尝试过更多时间吗?

标签: jquery html wait


【解决方案1】:

尝试使用 jquery when();

或尝试回调函数。

例如,如果你有 2 个方法 method1 和 method2 ..try

$.when(method1(), method2()).then(showData);​

只有在method1(),method2()执行完毕时才会执行showdata..

例子

function showData() {
    alert(something);

}

function method1() {
    //logic
}

function method2() {
  //logic  
}

$.when(method1(), method2()).then(showData);​

【讨论】:

  • 当它阻止我所有其他 jquery 动画时,我无法进入网站并到达我使用的地方
【解决方案2】:

使用 setTimeout,它会等待元素渲染。

设置超时(函数(){ $("#toZoom").elevateZoom({ zoomWindowFadeIn: 500, 缩放窗口淡出:500, 镜头淡入:500, 镜头淡出:500 }); }, 10);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 2016-06-18
    • 1970-01-01
    相关资源
    最近更新 更多