下午所有,我遇到了一个问题,我需要运行一个函数,然后完成后,运行下一个,并为四个函数执行此操作,我一直在这一段时间试图找到正确的语法布局我的函数调用,似乎无法找到任何东西来解决这个特定的情况。迫使一个javascript函数等待运行,直到第一个完成
HTML:
<div id = "putcontenthereafterajax">
</div><!--end putcontenthereafterajax-->
<div id = "putfooterhereafterajax">
</div<!--end putfooterhereafterajax-->
的jQuery:
$(window).load(function() {
function preload(arrayOfImages) {
$(arrayOfImages).each(function(){
$('<img/>')[0].src = this;
//alert("I cached "+this+"");
});
$('#progressbarinner').css("width","200px");//change the width of the inner progress bar div
}
function changecss(){
$('.hidetillcache').css("visibility","visible");//make the page visible
$('#loadingscreen').fadeOut("slow");
}
function contentajax(){
$.post("templates/content.php",
{
whatamidoing:"imgettingthecontent"
},
function(data){
$('#putcontenthereafterajax').after(''+data+'');
$('#progressbarinner').css("width","400px");//change the width of the inner progress bar div
});
}
function footerajax(){
$.post("templates/footer.php",
{
whatamidoing:"imgettingthefooter"
},
function(data){
$('#putfooterhereafterajax').after(''+data+'');
$('#progressbarinner').css("width","500px");//change the width of the inner progress bar div
});
}
preload([
'images/careers.jpg',
'images/careers.png',
'images/contact.jpg',
'images/facebook.png',
'images/footer.png',
'images/footerblack.png',
'images/footergrey.png',
'images/home.jpg',
'images/media.jpg',
'images/media.png',
'images/myitv3.jpg',
'images/newindex.jpg',
'images/newindex.png',
'images/services.jpg',
'images/twitter.png'
], contentajax(), footerajax(), csschanges());
});
基本上我已经陆续跑出一个填满了一下每个函数执行完毕后,这反过来requries每个功能的加载条按照正确的顺序,所有的函数都能正常工作,缓存和ajax甚至css的变化都能正常工作。但我似乎无法找到一种方法来强制他们以正确的顺序,并等待运行,直到前一个完成为了恭维加载栏。有人有主意吗?
在函数1的末尾调用函数2。'function1(){doStuff();函数2(); }' – 2013-04-30 14:29:06
@RickViscomi是不是在这个例子中做什么?有很多其他方法我试图 – Nick 2013-04-30 14:31:47
您可以尝试同步调用ajax函数: http://api.jquery.com/jQuery。ajax /#jQuery-ajax-settings – 2013-04-30 14:36:56