何を一時停止しようとしているがそれ以外の場合はループし続けるだろう関数であるならば、私は良い解決策を作ってみた:
HTML
<div id="stuff">Doing stuff</div>
<button id="pause">Pause/Resume</button>
JS
var paused = false;
document.getElementById('pause').addEventListener('click', function() {
paused = !paused;
if (!paused) {
next();
}
});
function doStuff() {
// Do whatever you want here, then invoke next() for the next iteration of that function, for example:
// (Note that the window.setTimeout is NOT part of the solution)
window.setTimeout(function() {
document.getElementById('stuff').append('.');
next();
}, 300);
}
function next() {
if (!paused) {
doStuff();
}
}
doStuff();
CodePen:https://codepen.io/liranh85/pen/baVqzY?editors=1010
'window.stopは()'スクリプトの実行を停止しませんので、いや、それはブラウザの停止ボタンに似ロードからウィンドウを、停止します。 – keune