简体   繁体   English

延迟在jQuery中的声明?

[英]delaying a statement in jquery?

i have two lines of javascript here, when the toggle function executes i want to delay before the find function happens. 我在这里有两行JavaScript,当执行切换功能时,我想在查找功能发生之前延迟。

$activeToggle.toggle("fast"); // i want this to run and then after 1250 millisec
$activeToggle.find(".anime").each(function (i,e){ // this code runs

        alert(e.id);

    });

I want to delay the second line of code to execute, because for some reason the alert is happening before the toggle action takes place, so their must a be way to delay the second line. 我想延迟第二行代码的执行,因为出于某种原因,警报是在切换动作发生之前发生的,因此它们必须是延迟第二行代码的一种方式。 thanks 谢谢

Make use of the callback function. 利用回调函数。 This ensures the toggle has finished executing before running the loop function you have. 这样可以确保在运行循环功能之前切换已完成执行。

$activeToggle.toggle("fast", function(){
   $(this).find(".anime").each(function (i,e){ // this code runs

        alert(e.id);

    });
})

This way you don't need to delay. 这样,您无需延迟。 Your code will be ran immediately after the toggle() function. 您的代码将在toggle()函数之后立即运行。

您可以使用delay()方法:

   $activeToggle.delay(1250).toggle("fast");
setTimeout( function () { $activeToggle.toggle("fast"); }, 1250);

Make use of jQuerys delay function :) 利用jQuerys延迟功能:)

$activeToggle.delay(1250).toggle("fast", function(){
   $(this).find(".anime").each(function (i,e){ // this code runs

        alert(e.id);

    });
})

Aswell as using a call back you can foce your code to wait before execution using .stop() 除了使用回调外,您还可以使用.stop()代码等待执行之前

$activeToggle.toggle("fast"); // i want this to run and then after 1250 millisec
$activeToggle.find(".anime").stop(true,true).each(function (i,e){ // this code runs
        alert(e.id);
});

If your goal is to trigger a function after the animation is complete, then you can pass toggle() a callback function that only runs after the animation is complete. 如果您的目标是在动画完成后触发一个函数,则可以传递toggle()一个仅在动画完成后才运行的回调函数。

$activeToggle.toggle("fast", function() {
    $activeToggle.find(".anime").each(

    function(i, e) {
        alert(e.id);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM