[英]Stop an animation with a button click using jquery
我試圖暫停正在運行的動畫,但我不知道自己在做什么。
我還嘗試了“函數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/
請幫助白痴。 先感謝您。
添加一個可以充當布爾值“ 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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.