DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> JavaScript綜合知識 >> javascript Deferred和遞歸次數限制實例
javascript Deferred和遞歸次數限制實例
編輯:JavaScript綜合知識     

 你知道Deferred和遞歸次數限制嗎?下面有個不錯的實例,大家可以看看

  1 2 3 4 5 6 7 8 9 10 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();  }   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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();  }   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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();  }
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved