我试图暂停正在运行的动画,但我不知道自己在做什么。
我还尝试了“ function stop(){clearInterval(changer)}”。
下面的HTML和JQUERY
```
<div id="change"></div>
<button id="stop">Stop</button>```
```
$(function() {
changer();
});
function changer() {
var words = ["a", "b", "c", "d", "e", "f", "g", "h"];
var idx = Math.floor(words.length * Math.random());
$('#change').text(words[idx]);
var time = Math.floor (100 * Math.random() + 75);
setTimeout(changer, time);
}
function stop() {
$("#change").stop(true);
}```
JSFIDDLE:https://jsfiddle.net/magoo/a6kt8qmf/1/
请帮助白痴。预先谢谢你。
答案 0 :(得分:0)
添加一个可以充当boolen'stopIT'的变量,在这种情况下,当调用stop函数时,无法调用setTimeout。
var stopIT = false;
function changer() {
var words = ["a", "b", "c", "d", "e", "f", "g", "h"];
var idx = Math.floor(words.length * Math.random());
$('#change').text(words[idx]);
var time = Math.floor (100 * Math.random() + 75);
stopIT === false && setTimeout(changer, time);
}
function stop() {
stopIT = true;
}