2017-06-21 182 views

回答

2

你不能“暂停”HTML的执行。您可以通过用户或使用计时器通过操作(按钮点击前的按钮)显示项目(最初使用css隐藏)来控制何时以及如何使用JavaScript的不同部分,以及使用javascript gets revealed。请参见暂停和播放的setTimeoutsetInterval

function removeClass(el, className) { 
 
    if (el.classList) 
 
    el.classList.remove(className); 
 
    else { 
 
    var reg = new RegExp('(\\s|^)' + className + '(\\s|$)'); 
 
    el.className = el.className.replace(reg, ' '); 
 
    } 
 
} 
 

 
function revealDivInTime(id, time) { 
 
    var div = document.getElementById(id); 
 
    if (div) { 
 
    setTimeout(function() { 
 
     removeClass(div, "hidden"); 
 
    }, time); 
 
    } 
 
} 
 

 
window.onload = function() { 
 
    document.getElementById("start").onclick = function() { 
 
    revealDivInTime("one", 1000); 
 
    revealDivInTime("two", 5000); 
 
    revealDivInTime("three", 10000); 
 
    } 
 
    document.getElementById("end").onclick = function() { 
 
    revealDivInTime("four", 0); 
 
    revealDivInTime("five", 3000); 
 
    revealDivInTime("three", 10000); 
 
    } 
 
}
.hidden { 
 
    display: none; 
 
}
<div> 
 
    Click to start story: <button id="start">click me</button> 
 
</div> 
 
<div id="one" class="hidden"> 
 
    <p>First Part of story revealed after 1 second</p> 
 
</div> 
 
<div id="two" class="hidden"> 
 
    <p>Second Part of story revealed after 5 seconds</p> 
 
</div> 
 
<div id="three" class="hidden"> 
 
    <p>Third Part of story revealed after 10 seconds</p> 
 
    <p> Click this button to finish Story: <button id="end">Finish</button> 
 
</div> 
 
<div id="four" class="hidden"> 
 
    <p>This get's revealed only after user clicks on button in story part 3. Story will end in 3 seconds</p> 
 
</div> 
 
<div id="five" class="hidden"> 
 
    <p>Story finished</p> 
 
</div>

实例表明:

var currentScene = 0, 
 
    looper, sceneCount = 10, 
 
    isPlaying = false; 
 

 
function removeClass(el, className) { 
 
    if (el.classList) 
 
    el.classList.remove(className); 
 
    else { 
 
    var reg = new RegExp('(\\s|^)' + className + '(\\s|$)'); 
 
    el.className = el.className.replace(reg, ' '); 
 
    } 
 
} 
 

 
function nextScene() { 
 
    currentScene++; 
 
    if (currentScene > sceneCount) { 
 
    clearInterval(looper); 
 
    isPlaying = false; 
 
    return; 
 
    } 
 
    removeClass(document.getElementById("s"+currentScene),"hidden"); 
 
} 
 

 
function play() { 
 
    if (isPlaying) { 
 
    return; 
 
    } 
 
    isPlaying = true; 
 
    looper = setInterval(nextScene, 2000); 
 
} 
 

 
function pause() { 
 
    if (!isPlaying) { 
 
    return; 
 
    } 
 
    isPlaying = false; 
 
    clearInterval(looper); 
 
} 
 

 
document.getElementById("play").onclick=function(){play();}; 
 
document.getElementById("pause").onclick=function(){pause();};
.hidden { 
 
    display: none; 
 
}
<button id="play">Play</button><button id="pause">Pause</button> 
 
<div id="s1" class="hidden"> 
 
    Scene 1 
 
</div> 
 
<div id="s2" class="hidden"> 
 
    Scene 2 
 
</div> 
 
<div id="s3" class="hidden"> 
 
    Scene 3 
 
</div> 
 
<div id="s4" class="hidden"> 
 
    Scene 4 
 
</div> 
 
<div id="s5" class="hidden"> 
 
    Scene 5 
 
</div> 
 
<div id="s6" class="hidden"> 
 
    Scene 6 
 
</div> 
 
<div id="s7" class="hidden"> 
 
    Scene 7 
 
</div> 
 
<div id="s8" class="hidden"> 
 
    Scene 8 
 
</div> 
 
<div id="s9" class="hidden"> 
 
    Scene 9 
 
</div> 
 
<div id="s10" class="hidden"> 
 
    Scene 10. Story Over. Click reload and click play to start again. 
 
</div>

+0

当我点击停止按钮时,如何暂停我的代码的一部分,并且只有当我点击播放按钮时才能继续。 –

+0

您可以使用setInterval和removeInterval实现暂停/播放效果。保持变量跟踪当前场景,并使用setInterval(以及稍后将存储引用删除它)来定期显示场景(同时保持currentScene变量更新以跟踪用户最后看到的场景)。当用户按下暂停时,使用setInterval(这将“暂停”)时获得的存储引用上的removeInterval删除间隔。当用户恢复时,再次调用setInterval以基于追踪最后看到的场景的变量的状态,定期显示场景。 –

+0

我无法理解这种方法。你能举一个例子吗? –