相思资源网 Design By www.200059.com

你知道Deferred和递归次数限制吗?下面有个不错的实例,大家可以看看

 function runAsyncTTS(text,speecher,audiopath) { 
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(); 
} 
function textToSpeechBat(metaJson, speecher, 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+'", "WordSpeed": "3", "UseCSSML": "0", "AudioPath": "'+audioFile+'"}'; 
jsons += args + "|"; 
} 
jsons = jsons.substr(0,jsons.length-1); 
tts.asyncTTSBat(jsons,function(err,ret) { 
result['resultJsonArr'] = ret.split('|'); 
def.resolve(result); 
}); 
return def.promise(); 
} 
function textToSpeechWithTryTimes(metaJson, speecher, audioPath,times) { 
var def = $.Deferred(); 
var ttsRet = null; 
var ttsCallBack = function(index) { 
if(index < times) { 
textToSpeechBat(metaJson,speecher,audioPath).done(function (ret) { 
console.log("textToSpeechWithTryTimes:"+JSON.stringify(ret)); 
ttsRet = ret; 
var resultJsonArr = ret.resultJsonArr; 
var audioFlag = true; 
for(var i=0;i<resultJsonArr.length;i++) { 
if(resultJsonArr[i] == "") { 
audioFlag = 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); 
} 
}); 
} 

if(index == times) { 
console.log("textToSpeechWithTryTimes timesover,return"); 
def.resolve(ttsRet); 
} 
}; 

ttsCallBack(0); 
return def.promise(); 
}
标签:
Deferred,递归

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“javascript Deferred和递归次数限制实例”

暂无javascript Deferred和递归次数限制实例的评论...