繁体   English   中英

Javascript 进度条与时间和回调完成

[英]Javascript progressbar with Time and Callback On Finish

我正在尝试获得一个将运行指定秒数的进度条,并在时间完成后显示一条消息。 到目前为止,我有以下代码仅显示进度条,没有时间或回调 function

var i = 0;
if (i == 0) {
i = 1;
var elem = document.getElementById("myBar");
var width = 1;
var id = setInterval(frame, 10);
function frame() {
  if (width >= 100) {
    clearInterval(id);
    i = 0;
  } else {
    width++;
    elem.style.width = width + "%";
  }
}
}

如何在上面的代码中传递时间和进度条上的回调完成

这是我为上传文件进度条 HTML 所做的示例

<form id="form" name="form" action="/uploader" enctype="multipart/form-data" 
method="post">
   <div class="buttons">
      <div class="upload-button">
       <div class="label">Click me!</div>
     <input id="files" name="files" type="file" size="1" multiple 
     onchange="uploadFiles('files');" />
    </div>
  </div>
</form>
<div id="progress" class="progress"><div id="bar" class="bar"></div><div id="label" 
class="label">0%</div></div>

js 触发 ajax 请求返回值以在 html 的进度栏中更新它

function uploadFiles(inputId) {
var input = document.getElementById(inputId);
var files = input.files;
var formData = new FormData();

for (var i = 0; i != files.length; i++) {
   formData.append("files", files[i]);
}

startUpdatingProgressIndicator();
  $.ajax(
    {
      url: "/uploader",
      data: formData,
      processData: false,
      contentType: false,
      type: "POST",
      success: function (data) {
      stopUpdatingProgressIndicator();
      alert("Files Uploaded!");
    }
   }
 );
}

var intervalId;

function startUpdatingProgressIndicator() {
$("#progress").show();

 intervalId = setInterval(
 function () {
  // We use the POST requests here to avoid caching problems (we could use the GET 
  requests and disable the cache instead)
     $.post(
        "/uploader/progress",
         function (progress) {
         $("#bar").css({ width: progress + "%" });
         $("#label").html(progress + "%");
         }
        );
       },
      10
     );
    }

 function stopUpdatingProgressIndicator() {
   clearInterval(intervalId);
 }

css

.upload-button {
  background: #f0f0f0;
  border-radius: 100px;
  text-align: center;
  position: relative;
  width: 200px;
  height: 200px;
 }

 .upload-button .label {
  line-height: 200px;
  pointer-events: none;
  position: absolute;
  left: 0;
  top: 0;
  width: 200px;
  height: 200px;
 }

 .upload-button input {
  opacity: 0;
  cursor: pointer;
  font-size: 200px;
  width: 200px;
  height: 200px;
 }

.progress {
  background: #f0f0f0;
  margin-top: 30px;
  display: none;
  position: relative;
 }

.progress .bar {
  background: green;
  width: 33%;
  height: 30px;
 }

 .progress .label {
  line-height: 30px;
  text-align: center;
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 30px;
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM