1
出於某種原因,clearTimeout函數未在此腳本工作
腳本將
1)自動播放整個YouTube視頻,並使用突出在產品菜單中的積極一步setTimeout繼續下一步。這工作正常
2)當點擊菜單中的某個步驟時,應停止自動播放。
- 第1步:轉到YouTube視頻的第二個15 - 玩,直到第二個25
- 步驟2:轉到YouTube視頻的每秒25 - 玩到第二35
當你在一個菜單中點擊某些步驟setTimeout函數仍然運行,幾秒鐘後gotostep函數被調用,並且電影繼續錯誤的步驟。Cleartimeout JavaScript的工作不
請任何人都可以檢查嗎?
<script type="text/javascript">
$(document).ready(function(){
gotostep('C9x4vG_6Qcs','1','1');});
function gotostep(youtube,step,auto){
var steparray=[15,25,35,48,58,65,79];
var numbersteps=steparray.length-1;
var i=1;
var nextstep=parseInt(step)+1;
while(i<=numbersteps){
if(step==i){
document.getElementById('step'+i+'').style.background='url("bg-button-active.jpg")';
var timeout=steparray[step]*1000-steparray[step-1]*1000+3000;
if(nextstep<=numbersteps && auto==1){
var timer1=setTimeout('gotostep("'+youtube+'","'+nextstep+'","1")',timeout);
}
if(nextstep<=numbersteps && auto==0){
clearTimeout(timer1);
}
}else{
document.getElementById('step'+i+'').style.background='url("bg-button.jpg")';
}
i++;
}
var thestart=parseInt(steparray[step-1]);
var theend=steparray[step];
document.getElementById('productdemo-iframe').src="http://www.youtube.com/embed/"+youtube+"?autoplay=1&rel=0&start="+thestart+"&end="+theend+"";
}
HTML代碼
<div id="container">
<div id="left">
<iframe width="560" id="productdemo-iframe" height="315" src="http://www.youtube.com/embed/C9x4vG_6Qcs?rel=0&start=0" frameborder="0" allowfullscreen></iframe>
</div>
<div id="right">
<img src="logobw.jpg" alt="Logo" />
<ul id="productdemo">
<li><a id="step1" href="#" onclick="gotostep('C9x4vG_6Qcs','1','0');">1. Power button</a></li>
<li><a id="step2" href="#" onclick="gotostep('C9x4vG_6Qcs','2','0');">2. Warming up</a></li>
<li><a id="step3" href="#" onclick="gotostep('C9x4vG_6Qcs','3','0');">3. Insert cover</a></li>
<li><a id="step4" href="#" onclick="gotostep('C9x4vG_6Qcs','4','0');">4. Cool down cover</a></li>
<li><a id="step5" href="#" onclick="gotostep('C9x4vG_6Qcs','5','0');">5. Insert second cover</a></li>
<li><a id="step6" href="#" onclick="gotostep('C9x4vG_6Qcs','6','0')">6. Turn off machine</a></li>
</ul>
</div>
</div>
由於它的工作! –
@MichaelV另一件事;使用getDuration()來查看視頻播放的距離並使用它突出顯示菜單按鈕不是更好嗎?如果用戶暫停視頻,那麼您的代碼暫時失敗了嗎?以下是api文檔:https://developers.google.com/youtube/js_api_reference – HMR