【发布时间】:2017-05-12 21:50:30
【问题描述】:
我正在使用文件堆栈做一个单词计数器文件,我想在它计算单词时显示加载,并在完成时隐藏它。
所以我在 HTML 中有这个:
<div id="loading" style="display:none;clear:both;">
<img src="loading.gif">
</div>
<input id="ff" type="filepicker" data-fp-apikey="<myAPI>" data-fp-mimetypes="image/*,application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document" data-fp-container="modal" data-fp-multiple="true" data-fp-button-class="btn btn-primary l-align" data-fp-button-text="Upload" data-fp-services="SKYDRIVE,COMPUTER,URL,GOOGLE_DRIVE,GMAIL" data-fp-language="es">
<div id="result"></div>
在 JS 中我有这个:
$("document").ready(function(){
$("#ff").change(function(){
var out='';
for(var i=0;i<event.fpfiles.length;i++){
var n = i+1;
out+='<a target=_blank href=' + event.fpfiles[i].url + '>' + '<button type=button class=btn-sc2><i class=fa fa-file-text aria-hidden=true></i> ' + event.fpfiles[i].filename + '</button></a>';out+=' '
if(event.fpfiles[i].mimetype=="image/jpeg"){
$.post("filecount.php",{
url: event.fpfiles[i].url
}).done(function(response){
//functions to count words
});//post
} else if(event.fpfiles[i].mimetype=="application/msword"){
var link = event.fpfiles[i].url;
var idfile = link.substr(link.lastIndexOf("/")+1);
var lcconvert = "https://process.filestackapi.com/output=format:txt/"+idfile;
$("#dkd").load(lcconvert, function(){
$.post("filecount2.php",{
url: $("#dkd").val()
}).done(function(response){
//functions to count words
});//post
});
}
};//for
$("#result").html(out);
});//#ff
});
我正在尝试输入 '$("#loading").show();'和'$("#loading").hide();'在发布功能的任何部分,但它不起作用或仅在发布功能完成时出现但不隐藏。
我需要一些帮助。
【问题讨论】:
-
你应该把它隐藏在
always函数中而不是done因为可能帖子失败了,always处理成功和失败 -
您需要将循环的每次迭代中的 AJAX 承诺存储到一个数组中(我们称之为
ajaxArray),然后使用$.when.apply($, ajaxArray)来检查所有 AJAX 请求何时完成完成。
标签: javascript jquery for-loop loading