简体   繁体   English

jQuery / Javascript函数延迟运行,直到另一个函数完成(如果需要)

[英]jQuery/Javascript Function delays running until another function finishes (if needed)

so I'm trying to use some javascript to do some simple fadeIn/fadeOut animations of some text divs. 因此,我尝试使用一些JavaScript来对某些文本div进行一些简单的fadeIn / fadeOut动画。 While I'm sure there is probably a better way to write my functions, they currently do work but with a minor issue that I'd like to fix and not sure of the best approach. 虽然我确定可能有更好的方法来编写我的函数,但是它们目前可以正常工作,但是有一个小问题需要解决,我不确定最好的方法。

$('#portfolio-link').click(function(){
   $( "#resume" ).fadeOut(500);
   $( "#profile" ).fadeOut(500);
   $( "#contact" ).fadeOut(500);
   setTimeout(function(){$( "#portfolio" ).fadeIn(500);},500);
}); 

$('#resume-link').click(function(){
   $( "#portfolio" ).fadeOut(500);
   $( "#profile" ).fadeOut(500);
   $( "#contact" ).fadeOut(500);
   setTimeout(function(){$( "#resume" ).fadeIn(500);},500);
}); 

$('#profile-link').click(function(){
   $( "#portfolio" ).fadeOut(500);
   $( "#resume" ).fadeOut(500);
   $( "#contact" ).fadeOut(500);
   setTimeout(function(){$( "#profile" ).fadeIn(500);},500);
}); 

$('#contact-link').click(function(){
   $( "#portfolio" ).fadeOut(500);
   $( "#resume" ).fadeOut(500);
   $( "#profile" ).fadeOut(500);
   setTimeout(function(){$( "#contact" ).fadeIn(500);},500);
}); 

Just to explain the code a little bit; 只是解释一下代码; the #portfolio-link, #resume-link, etc. are my navigation links. #portfolio-link,#resume-link等是我的导航链接。 When those are clicked I want them to fade out the text div currently showing (#portfolio, #resume, etc.) and replace it with the new div (For example if I click the portfolio nav link, I want to fade out the other 3 and fade in the porfolio text div). 单击这些按钮时,我希望它们淡出当前显示的文本div(#portfolio,#resume等),并将其替换为新的div(例如,如果我单击Portfolio nav链接,则希望淡出另一个3,然后在组合文本div中淡入)。

The problem I'm having is sometimes there are multiple divs showing if you click the nav links too quickly because the other function hasn't finished before the new one starts so I'm just wondering what the best approach to fixing this would be. 我遇到的问题是有时会显示多个div,如果您单击导航链接的速度过快,因为另一个功能在新功能启动之前尚未完成,因此我想知道解决此问题的最佳方法是什么。

Perhaps setting some sort of variable to a value of 1 at the beginning of the function and 0 when it's finished, then checking for the value to be 0 before allowing a new function to begin? 也许在函数的开头将某种变量的值设置为1,在函数结束时将其设置为0,然后在允许新函数开始之前检查该值是否为0? If that is a good approach, would it create a lag between functions (leaving the potential for a lot of delayed fading going on) or just not do anything on a click? 如果那是一个好方法,它会在功能之间造成滞后(留下大量延迟衰落的可能性)还是只是单击不做任何事情?

Any help on this is appreciated as I am a novice at best with javascript/jquery. 对此的任何帮助都将受到赞赏,因为我充其量是javascript / jquery的新手。

Thanks 谢谢

You should use .finish() jquery function, which is the equivalent of .stop(true,true). 您应该使用.finish()jQuery函数,该函数等效于.stop(true,true)。 This will ensure that all previous animation on the object is finished and it is in the final position. 这将确保该对象上的所有先前动画均已完成并且处于最终位置。 I'm going to show you the code for one of them, this will apply for all the other elements. 我将向您展示其中之一的代码,这将适用于所有其他元素。

$('#portfolio-link').click(function(){
   $( "#resume" ).finish().fadeOut(500);
   $( "#profile" ).finish().fadeOut(500);
   $( "#contact" ).finish().fadeOut(500);
   setTimeout(function(){$( "#portfolio" ).fadeIn(500);},500);
}); 

UPDATE UPDATE

The easiest way to achieve that is to add a class to all of the elements (contact,profile,resume,portfolio), let's call it class div_content then use: 最简单的方法是在所有元素(联系人,个人资料,简历,投资组合)中添加一个类,让我们将其称为类div_content然后使用:

$('#portfolio-link').click(function(){
   $( ".div_content" ).finish().fadeOut(500).promise().done(function(){$("#portfolio").fadeIn(500)});
}); 

Updated fiddle 更新小提琴

The fadeOut jquery function takes a second argument that is a function to run at completion so: fadeOut jQuery函数采用第二个参数,该参数是在完成时运行的函数,因此:

   $('#portfolio-link').click(function(){
   $( "#resume" ).fadeOut(500);
   $( "#profile" ).fadeOut(500);
   $( "#contact" ).fadeOut(500, function() {
       $( "#portfolio" ).fadeIn(500)
   });

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

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