jQuery倒计时计时器失败



我需要在循环中向用户展示一些问题,并且在显示问题时需要显示这样的倒数计时器:

function progress(timeleft, timetotal, $element) {
    var progressBarWidth = timeleft * $element.width() / timetotal;
    $element.find('div').animate({ width: progressBarWidth }, timeleft == timetotal ? 0 : 1000, 'linear');
    if(timeleft > 0) {
        setTimeout(function() {
            progress(timeleft - 1, timetotal, $element);
        }, 1000);
    }
};
$('document').ready(function() {
  $('#start').click(function() {
    progress(5, 5, $('#progressBar'));
  });
});
#progressBar {
  width: 90%;
  margin: 10px auto;
  height: 22px;
  background-color: #0A5F44;
}
#progressBar div {
  height: 100%;
  text-align: right;
  padding: 0 10px;
  line-height: 22px; /* same as #progressBar height if we want text middle aligned */
  width: 100%;
  background-color: #CBEA00;
  box-sizing: border-box;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<button id='start'>Start</button>
<div id="progressBar">
  <div></div>
</div>

问题是:如果我激活计时器(在小提琴中按下开始按钮(在开始并下降时,它会开始做疯狂的事情:上下行动。我想这与setTimeout()的递归性质有关。每次重新开始重新定位计时器(progressbar函数(?

您必须清除超时。这样:

function progress(timeleft, timetotal, $element) {
    var progressBarWidth = timeleft * $element.width() / timetotal;
    $element.find('div').animate({ width: progressBarWidth }, timeleft == timetotal ? 0 : 1000, 'linear');
    if(timeleft > 0) {
    if (window.timer) {
      clearTimeout(window.timer)
    }
    window.timer = setTimeout(function() {
            progress(timeleft - 1, timetotal, $element);
        }, 1000);
    }
};
$('document').ready(function() {
  $('#start').click(function() {
    progress(5, 5, $('#progressBar'));
  });
});
#progressBar {
  width: 90%;
  margin: 10px auto;
  height: 22px;
  background-color: #0A5F44;
}
#progressBar div {
  height: 100%;
  text-align: right;
  padding: 0 10px;
  line-height: 22px; /* same as #progressBar height if we want text middle aligned */
  width: 100%;
  background-color: #CBEA00;
  box-sizing: border-box;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<button id='start'>Start</button>
<div id="progressBar">
  <div></div>
</div>

将超时存储在变量中并在要重新开始时清除它。

var animating = null;
function progress(timeleft, timetotal, $element) {
    var progressBarWidth = timeleft * $element.width() / timetotal;
    $element.find('div').animate({ width: progressBarWidth }, timeleft == timetotal ? 0 : 1000, 'linear');
    if(timeleft > 0) {
        clearTimeout(animating);
        animating = setTimeout(function() {
            progress(timeleft - 1, timetotal, $element);
        }, 1000);
    }
};
$('document').ready(function() {
  $('#start').click(function() {
    progress(5, 5, $('#progressBar'));
  });
});
#progressBar {
  width: 90%;
  margin: 10px auto;
  height: 22px;
  background-color: #0A5F44;
}
#progressBar div {
  height: 100%;
  text-align: right;
  padding: 0 10px;
  line-height: 22px; /* same as #progressBar height if we want text middle aligned */
  width: 100%;
  background-color: #CBEA00;
  box-sizing: border-box;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<button id='start'>Start</button>
<div id="progressBar">
  <div></div>
</div>

最新更新