dragam0217 2017-02-23 08:53
浏览 39

进度条在任何Web浏览器上都不能正常工作

I´m having several problems with a progress bar in one of my developments.

I have a JavaScript function which launch serveral Ajax calls which call a PHP process, all at the same time. Those Ajax calls represent a percentage of a bigger proccess, so, for example, if I launch 100 ajax calls each one represent a 1% of the process.

The problem is that I can't force re-draw the HTTP DOM via JavaScript when an Ajax call ends.

So, the imports are these:

<link rel="stylesheet" href="(...)/javascript/themes/base/ui.core.css">
<link rel="stylesheet" href="(...)/javascript/themes/base/ui.theme.css">
<script src="(...)/js/jquery-ui-1.7.2.custom.min.js" type="text/javascript"></script>

And the important code is the next one:

Creation of progressbar (before launch Ajax calls):

$("#progressbar").progressbar({max:100, value:0});
$("#progressbar").progressbar("enable");

Every Ajax call will modify a variable when they finish. When I launch all Ajax calls I create an Observer function with JavaScript setTimeout, which looks at that variable and get the percentage:

setTimeout (function(){
    (...)
    var percentage = Math.round(((proccessedLines)/totalLines)*100);
    $("#progressbar").progressbar("value", percentage);
    (...)
}, 500);

And, finally, that $("#progressbar") is just a div HTML tag with that name.

So, the problem is that timeout. It supposed to update that progressbar value and update the DOM, it should to be redrawn but It doesn´t work as It should.

Any ideas?

I have to point one more thing: this process was working perfectly 'till 2 days ago; could be some new changes in the browsers?

  • 写回答

1条回答 默认 最新

  • dtyqflrr775518 2017-02-23 10:54
    关注

    The setTimeout function is just executed one time after 500ms. Maybe use the setInterval function to execute your function every 500ms or consider this snippet: https://jsfiddle.net/nicolastorre/o35teyq3/32/

    // Parameters
    var queryList = new Array('code', 'js', 'php');
    var lenQueryList = queryList.length;
    var percentage = 0;
    var proceed = 0;
    var totalPercentage = 100;
    
    // Init progress bar
    $("#progressbar").progressbar({max:totalPercentage, value:percentage});
    $("#progressbar").progressbar("enable");
    
    for(var i = 0; i < lenQueryList; i++) {
      /*### setTimeout to make a pause (just for the demo) ###*/
      setTimeout(function() {
        console.log('just a pause to see slowly!');
            console.log(queryList[i]);
    
            // Wikipedia api search
        $.ajax({
           url: 'https://en.wikipedia.org/w/api.php?callback=?',
           data: {
                srsearch: queryList[i],
                action: "query",
                list: "search",
                format: "json"
           },
           error: function() {
              console.log('An error occured');
           },
           dataType: 'json',
           success: function(data) {
                // Get results
              console.log(data); 
    
              // Update progress bar
              proceed++;
              percentage = Math.round((proceed/lenQueryList)*totalPercentage);
              $("#progressbar").progressbar("value", percentage);
           },
           type: 'POST'
        });
    
    
      }, 500 * i); // setTimeout to make a pause (just for the demo)
      /*########################################*/
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等