我叫一个函数:
$(document).on('click', '#home', function() {
runfunction2('', ''); // for stopping speech
runfunction( 956, 0 );
})
从这个函数我用语音调用另一个函数。 (https://github.com/vilic/cordova-plugin-tts)
function runfunction( post_id, step ){
runfunction2('example text', 'starttimer', post_id, step );
}
function runfunction3( text, end, post_id, step ){
TTS.speak({
text: text,
locale: 'de-DE',
rate: 1.5
}, function () {
if ( end == "startrec" ) {
console.log("startrec gestartet");
mediaPlay3.play();
setTimeout(function() {
mediaPlay3.stop();
startRecognition( post_id, step );
}, 1000);
setTimeout( function(){
// Do something after 3 second
stopRecognition();
} , 4000 );
} else if ( end == "newtry" ){
console.log("newtry gestartet");
mediaPlay3.play();
setTimeout(function() {
mediaPlay3.stop();
startRecognition( post_id, step );
}, 1000);
setTimeout( function(){
// Do something after 3 second
stopRecognition();
} , 4000);
} else if ( end == "starttimer" ){
timer(post_id, step );
} else {
//
}
}
如果我在此过程中再次单击主页按钮,我想从头开始。问题是第一个函数尚未完成并且在后台运行(仍然调用第二个函数)。但我想取消所有这一切,让一切从一开始就运行。怎么可能?
答案 0 :(得分:1)
下面的代码是模拟线程,您可以使用此代码。 HTML:
<button type="button" class="btn btn-primary" id="btnStart">Start</button>
<button type="button" class="btn btn-default" id="btnCancel">Cancel</button>
JS:
$(document).ready(function () {
$('#btnStart').on('click', infinityFunction);
$('#btnCancel').on('click', cancelProcess);
});
var busy = false;
var cancelled = false;
var processor = [];
var countN = 0;
function infinityFunction() {
if(cancelled){
if(processor.length > 0){
clearTimeout(processor[0]);
processor.splice(0,1);
}
busy = false;
cancelled = false;
countN = 0;
return;
}
if(!busy) {
busy = true;
console.log('Processing some code :'+countN);
countN++;
if(processor.length > 0){
clearTimeout(processor[0]);
processor.splice(0,1);
}
var myProcessor = setInterval(infinityFunction, 1000);
busy = false;
processor.push(myProcessor);
console.log(processor);
}else{
console.log('I am busy now');
}
}
function cancelProcess() {
cancelled = true;
}