要停止进度动画,请考虑使用 该 clearInterval() 方法 它允许您停止驱动增量动画的基础间隔。
clearInterval()
该 clearInterval() method接受一个interval id(这是一个值) setInterval() 函数返回),并使相应的间隔回调停止运行。
setInterval()
要将它与您的代码集成,您可以拉动 var id 在...之外 autoProgress() 功能,以便它可以访问 stopProgress() (你打电话的地方 clearInterval() )。此外,作为安全措施,请考虑清除您的运行中存在的任何现有间隔 id 在开始一个新变量之前变量 - 防止一次运行多个间隔(这可能会搞乱你的进度动画)。
var id
autoProgress()
stopProgress()
id
见 /*Update:*/ 以下代码段中的注释可获得更多详细信息:
/*Update:*/
//Get Current Value of attributes function getProgress() { return document.getElementById("progressbar").getAttribute("aria-valuenow"); return document.getElementById("progressbar").getAttribute("style","width:"); return document.getElementById("progressbar").innerHTML; } //Set value of attributes function setProgress(value) { document.getElementById("progressbar").setAttribute("aria-valuenow",value); document.getElementById("progressbar").setAttribute("style","width: " +value+ "%"); document.getElementById("progressbar").innerHTML = (value+ "%"); } //Call get function assign a variable to this, When value is less than 100 value increases by 1. function increment() { var i = getProgress(); if(i < 100){ i++; setProgress(i); }else{ alert("Progress Complete!"); //Alert presents itself once the value reaches max value. } } //Decrease current value by -1. function decrement() { var d = getProgress(); setProgress(d - 1); } //Current value set back to 0. function resetButton() { var r = getProgress(); setProgress(r = 0); } /* Update: Move id out side of autoProgress() */ var id; //Auto complete value to max in this case max is 100 with Interval of 100. function autoProgress() { var elem = document.getElementById("progressbar"); var width = 0; /* Update: stop any previous interval if one exists, and set new interval */ stopProgress(); id = setInterval(frame, 100); function frame(){ if(width == 100){ clearInterval(id); }else{ width++; elem.style.width = width + '%'; elem.innerHTML = width * 1 + '%'; } } } //Stop Auto complete value at current value when button is pressed. function stopProgress(){ /* Update: Stop id interval */ clearInterval(id) id = '' }
<!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous"> <title>Progress Bar</title> </head> <body> <!-- Container --> <div class="container"> <h1>This Process bar is animated using <br>JavaScript!</h1> <br> <!-- Div For Progress Bar --> <div class="progress"> <div class="progress-bar progress-bar-striped" role="progressbar" style="width: 0%;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" id="progressbar" >0%</div> </div> <br> <!-- Buttons --> <button type="button" class="btn btn-primary" onclick = "increment()">Increment</button> <button type="button" class="btn btn-dark" onclick="resetButton()">Reset</button> <button type="button" class="btn btn-success" onclick="decrement()">Decrement</button> <button type="button" class="btn btn-warning" onclick="autoProgress()">Start Auto Progress!</button> <button type="button" class="btn btn-danger" onclick="stopProgress()">Stop Auto Progress!</button> <!-- End of Container --> </div> <!-- Optional JavaScript --> <!-- jQuery first, then Popper.js, then Bootstrap JS --> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script> <script src="Assignment4.js"></script> </body> </html>
其实你几乎就在那里。 为了能够停止间隔,您需要在变量中存储引用。 这就是你在这里所做的事情:
var id = setInterval(frame, 100);
不幸的是,这个变量可以在你的内部看到 的 autoProgress 强> 功能。 因此,首先在函数之外全局声明它
var id;
并在里面这样称呼它 的 autoProgress 强> 功能
id = setInterval(frame, 100);
现在你可以简单地停止你内部的间隔 的 stopProgress 强> 通过调用功能
clearInterval(id);