简体   繁体   English

SetTimeout递归(JavaScript)

[英]SetTimeout recursion (Javascript)

The code below recursively plays an audio element and tracks iterations to text, both of which are stopped via a button that clears their timeouts. 下面的代码递归播放音频元素,并跟踪到文本的迭代,两者均通过清除超时的按钮停止。

If playTimeout is assigned to external function nextThing , when the stop button is clicked the audio will play once more after the text iterations stop. 如果将playTimeout分配给外部函数nextThing ,则单击停止按钮后,在文本迭代停止后,音频将再次播放。 However in the commented (internal) version of playTimeout, the audio stops right away. 但是,在带注释的(内部)playTimeout版本中,音频立即停止。

Questions: a) why is this happening? 问题:a)为什么会这样? and b) how can I properly phrase this so iterations and audio move together? b)我如何恰当地表达这一点,以便迭代和音频一起移动?

function nextThing(millis,pitch){
    setTimeout(playTone,millis,pitch);
};
function timedCount(millis){
    document.getElementById('txt').value=iteration;
    playTimeout=nextThing(millis,"C3");     
//  playTimeout=setTimeout(playTone,millis,"C3")
    doRecursion=setTimeout(function(){timedCount(millis)},millis);      
    iteration++;
    console.log("made it");
}

Your nextThing function didn't return anything, the timer id is ignored and it would assign undefined to playTimeout . 您的nextThing函数未返回任何内容,计时器id被忽略,​​它将为undefined分配给playTimeout

function nextThing(millis, pitch) {
    return setTimeout(playTone, millis, pitch);
//  ^^^^^^
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM