/*** * 讲笑话函数(调试用) * @param callback 回调函数 */ function randomText(callback) { var result =''; $.ajax({ type:"get", url:"http://www.mxnzp.com/api/jokes/list/random", dataType:"json", async:false, success:function(response){ var code = response.code; var data = response.data; if(code == 1 && data.length > 1) { var text = data[0].content; callback(text); result = text; } },error:function (res) { console.log("调用笑话接口失败",res); } }) return result; }
调用方法 /*** * 注册事件处理委托 */ $("#convert").click(function(){ textToSpeech(randomText( function(text){ //调试过程中往文本上添加笑话内容 $("textarea#text").text(text); } ),function (state,response) { switch (state) { case 'fail': console.error("调用失败,",response); break; case 'success': onSuccess(response); break; } }); });