你知道Deferred和递归次数限制吗?下面有个不错的实例,大家可以看看
diopath) {
var def = jQuery.Deferred();
var args = {"SynthText": text,"VoiceSpeecher": speecher,"WordSpeed": "3","UseCSSML": "0","AudioPath": audiopath};
tts.asyncTTS(JSON.stringify(args),function(err,result) {
def.resolve(result);
});
return def.promise();
}
MetaJson,audioPath) {
var def = $.Deferred();
var result = {originalWords:"",resultJsonArr:[]};
var jsons="";
for(var index=0;index < MetaJson.words.length;index++) {
var audioName = MetaJson.words[index]['audio'];
audioName = audioName.replace('.mp3','');
var audioFile = audioPath +"/" + audioName + '.wav';
var args = '{"SynthText": "'+MetaJson.words[index]['word']+'","VoiceSpeecher": "'+speecher+'","AudioPath": "'+audioFile+'"}';
jsons += args + "|";
}
jsons = jsons.substr(0,jsons.length-1);
tts.asyncTTSBat(jsons,ret) {
result['resultJsonArr'] = ret.split('|');
def.resolve(result);
});
return def.promise();
}
MetaJson,audioPath,times) {
var def = $.Deferred();
var ttsRet = null;
var ttsCallBack = function(index) {
if(index < times) {
textToSpeechBat(MetaJson,audioPath).done(function (ret) {
console.log("textToSpeechWithTryTimes:"+JSON.stringify(ret));
ttsRet = ret;
var resultJsonArr = ret.resultJsonArr;
var audioFlag = true;
for(var i=0;idioFlag = false;
break;
}
var retObj = JSON.parse(resultJsonArr[i]);
console.log(retObj['audioFlag']);
if(retObj['audioFlag'] == 'false' || retObj['result']=="") {
audioFlag = false;
break;
}
}
console.log(audioFlag);
if(audioFlag == false) {
console.log("textToSpeechWithTryTimes Fail,try again!");
ttsCallBack(++index);
}else {
console.log("textToSpeechWithTryTimes succeed,return");
def.resolve(ret);
}
});
}
原文链接:https://www.f2er.com/js/57480.htmlif(index == times) {
console.log("textToSpeechWithTryTimes timesover,return");
def.resolve(ttsRet);
}
};
ttsCallBack(0);
return def.promise();
}