javascript – 在函数完成时执行代码
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 在函数完成时执行代码,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6133字,纯文字阅读大概需要9分钟。
内容图文
我的代码中有一个函数可以激活几个元素.我想要做的是执行一系列代码,但只有在此功能完成后才能执行.
比方说,我有这个:
executeMyAnimationFunction();
// Execute this code AFTER executeMyAnimationFunction() has completed
alert('test');
到目前为止,我已尝试使用setTimeout,但这似乎不太可靠.我也尝试过使用.promise(),但这似乎也没有正确执行:
$(executeMyAnimationFunction()).promise().done(function () {
alert('test');
});
但是我承认我错误地使用它.在jquery站点上的the demo中,似乎promise()正在被动画化的元素上使用.我尝试这样做:
$('#myAnimatedElement').promise().done(function () {
alert('test');
});
它似乎在某些情况下有效,但在其他情况下则不起作用.我想知道这是否是使用promise()的可靠方式?
基本上我现在已经陷入了如何做到这一点.我想做的就是等到函数完全执行(函数只是动画一组元素),然后继续执行我的代码.
有没有一种简单的方法来使用我缺少的jquery / javascript?
谢谢
编辑:为了更详细地显示我的问题,这里是我正在使用的确切代码:
<script>
$(function () {
$(window).bind('hashchange', function (e) {
if ($(':animated').length) {
return false;
}
var section = $.param.fragment();
var current = $('#content').children(':visible').attr('id');
$('a').removeClass('active');
// If this is the first load of the page, then animate in the initial content
if (section === '') {
if (current === 'reception') {
animateContentIn("reception");
}
else {
$('a[href="#' + section + '"]').addClass('active');
// Animate out existing content
animateContentOut(current);
setTimeout(function () {
animateContentIn("reception");
}, 1000);
}
}
else {
// Otherwise find the current page content and animate out
animateContentOut(current);
setTimeout(function () {
animateContentIn(section);
}, 1000);
}
})
// Since the event is only triggered when the hash changes, we need to trigger
// the event now, to handle the hash the page may have loaded with.
$(window).trigger('hashchange');
});
function animateContentIn(activePage) {
// Now animate in the new content
switch (activePage) {
case "floorspace":
animateFloorspaceElementsIn();
break;
case "reception":
animateReceptionElementsIn();
break;
}
}
function animateContentOut(currentPage) {
// Now animate in the new content
switch (currentPage) {
case "floorspace":
animateFloorspaceElementsOut();
break;
case "reception":
animateReceptionElementsOut();
break;
}
}
function animateReceptionElementsIn() {
$('#reception').show();
$('#reception .title').animate({
bottom: 520
}, 400);
$('#reception .tile1').animate({
bottom: 504
}, 600);
$('#reception .tile2').animate({
bottom: 504
}, 700);
$('#reception .hero').animate({
bottom: 40
}, 1000);
$('#reception .content1').animate({
bottom: 8
}, 400);
$('#reception .content2').animate({
bottom: 8
}, 500);
}
function animateReceptionElementsOut() {
$('#reception .title').animate({
bottom: -56
}, 400);
$('#reception .tile1').animate({
bottom: -136
}, 600);
$('#reception .tile2').animate({
bottom: -152
}, 700);
$('#reception .hero').animate({
bottom: -464
}, 1000, function () {
$('#reception').hide();
});
$('#reception .content1').animate({
bottom: -112
}, 400);
$('#reception .content2').animate({
bottom: -104
}, 500);
}
function animateFloorspaceElementsIn() {
$('#floorspace').show();
$('#floorspace .title').animate({
bottom: 520
}, 400);
$('#floorspace .tile1').animate({
bottom: 504
}, 600);
$('#floorspace .tile2').animate({
bottom: 504
}, 700);
$('#floorspace .hero').animate({
bottom: 40
}, 1000);
$('#floorspace .content1').animate({
bottom: 8
}, 400);
$('#floorspace .content2').animate({
bottom: 8
}, 500);
}
function animateFloorspaceElementsOut() {
$('#floorspace .title').animate({
bottom: -56
}, 400);
$('#floorspace .tile1').animate({
bottom: -136
}, 600);
$('#floorspace .tile2').animate({
bottom: -152
}, 700);
$('#floorspace .hero').animate({
bottom: -464
}, 1000, function () {
$('#floorspace').hide();
});
$('#floorspace .content1').animate({
bottom: -112
}, 400);
$('#floorspace .content2').animate({
bottom: -104
}, 500);
}
</script>
</head>
<body>
<div id="vert-wrapper">
<div id="wrapper">
<aside id="sidebar">
<nav id="navigation">
<ul>
<li><a href="#">Building</a></li>
<li><a href="#reception">Reception</a></li>
<li><a href="#floorspace">Floor Space</a></li>
<li><a href="#">Views</a></li>
<li><a href="#">Terraces</a></li>
<li><a href="#">Profile</a></li>
</ul>
<ul>
<li><a href="#">Specification</a></li>
<li><a href="#">Plans</a></li>
<li><a href="#">Location</a></li>
<li><a href="#">Ten Storeys</a></li>
<li><a href="#">Gallery</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</aside>
<div id="content">
<article id="reception">
<h1 class="title">
<img src="/images/reception/title.png" alt="Edge" />
</h1>
<img src="/images/reception/1.jpg" alt="" class="tile1" />
<img src="/images/reception/2.jpg" alt="" class="tile2" />
<img src="/images/reception/hero.jpg" alt="" class="hero" />
<img src="/images/reception/content1.jpg" alt="" class="content1" />
<img src="/images/reception/content2.jpg" alt="" class="content2" />
</article>
<article id="floorspace">
<h1 class="title">
<img src="/images/floorspace/title.png" alt="Space" />
</h1>
<img src="/images/floorspace/1.jpg" alt="" class="tile1" />
<img src="/images/floorspace/2.jpg" alt="" class="tile2" />
<img src="/images/floorspace/hero.jpg" alt="" class="hero" />
<img src="/images/floorspace/content1.jpg" alt="" class="content1" />
<img src="/images/floorspace/content2.jpg" alt="" class="content2" />
</article>
</div>
</div>
</div>
</body>
现在我上面的代码确实有效,但是我觉得它不是很干净,也不是100%的时间都能正确执行.
基本上我遇到的困境是我希望能够在animateContentOut()完成后执行animateContentIn().我设法做到这一点的唯一方法是将animateContentIn()函数延迟到执行最长动画所需的时间 – 在这种情况下它是1000ms.
是不是有更整洁,更有效的写作方式?
解决方法:
function someRandomFunction(){
// This will be executed after myFirstFunction
}
function myFirstFunction(callbackFunction){
// Do some stuff
alert(1);
// Do something slow here
// Callback when done:
callbackFunction(some, input);
}
// Usage:
myFirstFunction( someRandomFunction ); // with existing function
myFirstFunction( function(){ alert(1);} ); // anonymous
注意:您可以使用匿名函数
小演示:http://jsfiddle.net/zcp82/2/
另一个有良好信息的话题:Create a custom callback in JavaScript
内容总结
以上是互联网集市为您收集整理的javascript – 在函数完成时执行代码全部内容,希望文章能够帮你解决javascript – 在函数完成时执行代码所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。