JS 애니메이션이있는 간단한 진행 표시 줄이 있습니다. 20ms마다 진행률 막대의 값이 0.25 씩 증가합니다. 아래의 JSFiddle에서 볼 수 있듯이 진행 막대를 완료하는 데 20 * 4 * 100ms = 8 초가 걸립니다. 나도 같은 코드를 가지고 대신 0 % 50 %의 진행률 표시 줄을 시작하는 경우두 개의 setInterval이 같은 시간에 끝나도록하는 방법
function updateProgress(currentValue, expectedValue){
var inProgress = setInterval(function() {
currentValue = currentValue + 0.25;
$('#progress-bar').attr('value', currentValue);
$('#progress-text').text(Math.round(currentValue));
if (currentValue == expectedValue) clearInterval(inProgress);
}, 20);
}
updateProgress(0, 100);
<progress id="progress-bar" value="0" max="100"></progress>
<div><span id="progress-text">0</span>%</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
그래서, 그것은 진행률 표시 줄을 완료 4 초 20 * 4 * 50ms의의 = 걸릴 것입니다, 아래 JSFiddle에서 볼 수 있듯이. 나는이 기능을 원하는
function updateProgress(currentValue, expectedValue){
var inProgress = setInterval(function() {
currentValue = currentValue + 0.25;
$('#progress-bar').attr('value', currentValue);
$('#progress-text').text(Math.round(currentValue));
if (currentValue == expectedValue) clearInterval(inProgress);
}, 20);
}
updateProgress(50, 100);
<progress id="progress-bar" value="50" max="100"></progress>
<div><span id="progress-text">50</span>%</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
function updateProgress(currentValue, expectedValue){
var interval = 4000/(expectedValue - currentValue)/4;
var inProgress = setInterval(function() {
currentValue = currentValue + 0.25;
$('#progress-bar').attr('value', currentValue);
$('#progress-text').text(Math.round(currentValue));
if (currentValue == expectedValue) clearInterval(inProgress);
}, interval);
}
updateProgress(50, 100);
정확히 200ms의 지속 시간으로 0에서 100으로 가고 싶을 때 문제가 발생하므로 내 간격은 200/100/4 = 0.5ms입니다. 그리고 변호사는 200ms보다 더 오래 걸렸고 나는 그 이유를 찾지 못했습니다! 답변 주셔서 대단히 감사합니다. @skirtle – Vald