javascript Deferred和递归次数限制

  1. function runAsyncTTS(text,speecher,audiopath) {  
  2.         var def = jQuery.Deferred();  
  3.         var args = {“SynthText”: text, “VoiceSpeecher”: speecher, “WordSpeed”“3”“UseCSSML”“0”“AudioPath”: audiopath};  
  4.   
  5.         tts.asyncTTS(JSON.stringify(args),function(err,result) {  
  6.             def.resolve(result);  
  7.         });  
  8.   
  9.         return def.promise();  
  10.     }  

 

  1. function textToSpeechBat(metaJson, speecher, audioPath) {  
  2.         var def = $.Deferred();  
  3.         var result = {originalWords:“”, resultJsonArr:[]};  
  4.         var jsons=“”;  
  5.         for(var index=0;index < metaJson.words.length;index++) {  
  6.             var audioName = metaJson.words[index][‘audio’];  
  7.             audioName = audioName.replace(‘.mp3’,);  
  8.             var audioFile = audioPath +“/” + audioName + ‘.wav’;  
  9.             var args = ‘{“SynthText”: “‘+metaJson.words[index][‘word’]+‘”, “VoiceSpeecher”: “‘+speecher+‘”, “WordSpeed”: “3”, “UseCSSML”: “0”, “AudioPath”: “‘+audioFile+‘”}’;  
  10.             jsons += args + “|”;  
  11.         }  
  12.         jsons = jsons.substr(0,jsons.length-1);  
  13.         tts.asyncTTSBat(jsons,function(err,ret) {  
  14.             result[‘resultJsonArr’] = ret.split(‘|’);  
  15.             def.resolve(result);  
  16.         });  
  17.         return def.promise();  
  18.     }  

 

  1. function textToSpeechWithTryTimes(metaJson, speecher, audioPath,times) {  
  2.         var def = $.Deferred();  
  3.         var ttsRet = null;  
  4.         var ttsCallBack = function(index) {  
  5.             if(index < times) {  
  6.                 textToSpeechBat(metaJson,speecher,audioPath).done(function (ret) {  
  7.                     console.log(“textToSpeechWithTryTimes:”+JSON.stringify(ret));  
  8.                     ttsRet = ret;  
  9.                     var resultJsonArr = ret.resultJsonArr;  
  10.                     var audioFlag = true;  
  11.                     for(var i=0;i<resultJsonArr.length;i++) {  
  12.                         if(resultJsonArr[i] == “”) {  
  13.                             audioFlag = false;  
  14.                             break;  
  15.                         }  
  16.                         var retObj = JSON.parse(resultJsonArr[i]);  
  17.                         console.log(retObj[‘audioFlag’]);  
  18.                         if(retObj[‘audioFlag’] == ‘false’ || retObj[‘result’]==“”) {  
  19.                             audioFlag = false;  
  20.                             break;  
  21.                         }  
  22.                     }  
  23.                     console.log(audioFlag);  
  24.                     if(audioFlag == false) {  
  25.                         console.log(“textToSpeechWithTryTimes Fail, try again!”);  
  26.                         ttsCallBack(++index);  
  27.                     }else {  
  28.                         console.log(“textToSpeechWithTryTimes succeed,return”);  
  29.                         def.resolve(ret);  
  30.                     }  
  31.                 });  
  32.             }  
  33.   
  34.             if(index == times) {  
  35.                 console.log(“textToSpeechWithTryTimes timesover,return”);  
  36.                 def.resolve(ttsRet);  
  37.             }  
  38.         };  
  39.   
  40.         ttsCallBack(0);  
  41.         return def.promise();  
  42.     }  

标签