代码之家  ›  专栏  ›  技术社区  ›  Garrett Rose

如何在一个函数调用中返回进度条的两个百分比值?

  •  0
  • Garrett Rose  · 技术社区  · 7 年前

    我的登陆页面将有我所知道的每项技术的进度百分比。我唯一能解决的办法是,为每个函数生成两个值不同的函数,然后在体上调用它们。这看起来非常混乱。我怎么能把它做得更好?

    function webMove() {
      var elem = document.getElementById("myBar");
      var width = 0;
      var width2 = 0;
      var id = setInterval(frame, 10);
    
      function frame() {
        if (width >= 50) {
          clearInterval(id);
        } else {
          width++;
          elem.style.width = width + '%';
          elem.innerHTML = width * 1 + '%';
        }
      }
    }
    
    
    function gitHubMove() {
      var elem = document.getElementById("myBar2");
      var width = 0;
      var id = setInterval(frame, 10);
    
      function frame() {
        if (width >= 100) {
          clearInterval(id);
        } else {
          width++;
          elem.style.width = width + '%';
          elem.innerHTML = width * 1 + '%';
        }
      }
    }
    <body onload="webMove(),gitHubMove()">
      <div id="container">
        <div id="myProgress">
          <div id="myBar">
          </div>
        </div>
      </div>
      <div id="container2">
        <div id="myProgress2">
          <div id="myBar2">
          </div>
        </div>
      </div>
    </body>
    2 回复  |  直到 7 年前
        1
  •  0
  •   undg    7 年前

    你喜欢干代码,那是很好的习惯。在这里,您可以对代码进行一些小的修改:

    function progressBar(elementId, progressPercent){
        var elem = document.getElementById(elementId);
        var width = 0;
        var id = setInterval(frame, 10);
    
        function frame() {
            if (width >= progressPercent) {
                clearInterval(id);
            } else {
                width++;
                elem.style.width = width + '%';
                elem.innerHTML = width * 1 + '%';
            }
        }
    }
    progressBar("webMove", 80)
    progressBar("gitHubMove", 60)
        <div id="container">
            <div id="myProgress">
                <div id="webMove">
                </div>
            </div>
        </div>
        <div id="container2">
            <div id="myProgress2">
                <div id="gitHubMove">
                </div>
            </div>
        </div>
        2
  •  1
  •   undg    7 年前

    定义一个对象来跟踪你想要的。这是一个潜在的解决方案,你想要多少进度条,它的性能,因为只有一个间隔运行。我在当前未被使用的对象中添加了一个开始,但是如果你想让进程的开始是0以外的事情,那就是你如何做到这一点。

     function progress() {
            keys = ['web', 'github']
            var meta = {
              web: {
                done: false,
                current: 0,
                max: 50,
                start: 0,
                elem: document.getElementById("myBar")
              },
              github: {
                done: false,
                current: 0,
                max: 100,
                start: 0,
                elem: document.getElementById("myBar2")
              }
            }
            var id = setInterval(frame, 10);
            
            function frame() {
              var complete = true
              keys.forEach( function(key) {
                var obj = meta[key]
                if (!obj.done) {
                  complete = false
                }
                if (obj.current >= obj.max) {
                  obj.done = true
                  return
                }
        
                obj.current++
                obj.elem.style.width = obj.current + '%';
                obj.elem.innerHTML = obj.current * 1 + '%';
              })
              if (complete) {
                clearInterval(id)
              }
            }
        }
        progress()
    <div id="myBar"></div>
    <div id="myBar2"></div>