javascript Deferred和递归次数限制

Posted Brenda

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 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();
    }

以上是关于javascript Deferred和递归次数限制的主要内容,如果未能解决你的问题,请参考以下文章

jQuery.Deferred对象

高性能 JavaScriptの笔记-- 算法与流程控制

javascript异步代码的回调地狱以及JQuery.deferred提供的promise解决方式

如何用Redis实现访问次数限流?

数学家

几个免费API接口分享,调用完全不限次数...