简体   繁体   English

如何停止复制进度条?

[英]How to stop duplicating a progress bar?

The problem that I have over here is that whenever I update the progressbar value with a pos+1, it keeps on creating a new bar while adding the progress to the original progress bar. 我这里遇到的问题是,每当我用pos + 1更新进度条值时,它都会继续创建新栏,同时将进度添加到原始进度条中。 How do I solve the duplicate problem? 我该如何解决重复问题?

Here's a link to test my code. 这是测试我的代码的链接

var progbar = document.createElement("PROGRESS");
progbar.id = 'progress';
progbar.setAttribute("value", "1");
progbar.setAttribute("max", "10");
document.getElementById("status").appendChild(progbar);
document.getElementById("progress").value = (pos+1);

You example is given more information about the problem you are dealing with. 您的示例将获得有关您正在处理的问题的更多信息。

Basically, you have a single script to create a progressbar and to update the same progressbar. 基本上,您只有一个脚本来创建进度条并更新相同的进度条。 If you want to do both tasks in the same script, you need to do a check to see if the progress bar is already in your page before creating it. 如果要在同一脚本中执行这两项任务,则需要在创建进度条之前检查一下进度条是否已在页面中。

function myFunction(){
    //Check if the progress is already in the page to avoid creating it more than once
    if (!document.getElementById("progress")){
      var x = document.createElement("PROGRESS");

      x.setAttribute("id", "progress");
      x.setAttribute("value", "22");
      x.setAttribute("max", "100");
      document.body.appendChild(x);
    }

    //Not sure where the pos value was taken from... I assumed you wanted to increase the progressbar value by 1
    document.getElementById("progress").value++;
}

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

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