【问题标题】:Show a loading while a for loop is executing until finishes在 for 循环执行时显示加载直到完成
【发布时间】: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


【解决方案1】:

每当您执行任何 Ajax 调用时,ajaxStart/Stop 函数都会触发。

从 jQuery 1.8 开始,文档声明 .ajaxStart/Stop 只能附加到文档。这会将上面的 sn-p 转换为:

var $loading = $('#loadingDiv').hide();
$(document)
  .ajaxStart(function () {
    $loading.show();
  })
  .ajaxStop(function () {
    $loading.hide();
  });

在您的具体情况下:

$loading.show();
$.post("filecount.php",{
    url: event.fpfiles[i].url
}).success(function(response){
    //functions to count words
    $loading.hide();
});//post 

在调用之前显示它并在成功函数或错误函数中隐藏它。

【讨论】:

  • 我的网页中有另一个 $.post ajax,我如何仅使用这两个特定的 ajax $.post 来做到这一点?
  • 所以你只想用这两个显示它而不是每个 AJAX 请求?
  • 是的,因为我还有另一个按钮可以执行另一个 ajax 请求。
  • 它工作得非常接近,当我上传文件加载显示然后隐藏并且它仍在计数时,我想在计数完成后加载隐藏。
  • 你可以尝试成功处理程序而不是完成吗?看看我的编辑
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-21
  • 2017-02-24
  • 1970-01-01
  • 2015-03-04
  • 1970-01-01
相关资源
最近更新 更多