2017-04-02 49 views
-1

我試圖不成功地將我的數組作爲其結果傳遞給外部函數?這裏是jsbin中的工作代碼:https://jsbin.com/kugesozawi/edit?js,console,output結果應該傳遞給returnSearch。如何從我的函數傳遞數組返回

var googleSuggest = function(returnSearch){ 

    var term = $('#searchInput').val(); 
    var result = []; 
    var service = { 
    youtube: { client: 'youtube', ds: 'yt' }, 
    books: { client: 'books', ds: 'bo' }, 
    products: { client: 'products-cc', ds: 'sh' }, 
    news: { client: 'news-cc', ds: 'n' }, 
    images: { client: 'img', ds: 'i' }, 
    web: { client: 'hp', ds: '' }, 
    recipes: { client: 'hp', ds: 'r' } 
    }; 

    $.ajax({ 
    url: 'https://clients1.google.com/complete/search', 
    dataType: 'jsonp', 
    data: { 
     q: term, 
     nolabels: 't', 
     client: service.web.client, 
     ds: service.web.ds 
    } 
    }).done(function(data) { 

     result.pop() 

     $.each(data[1], function(item, value) { 

     var stripedValue = value[0].replace(/<[^>]+>/g, ''); 

      result.push(stripedValue); 

     }) 

     console.log(result) 

    }) 

    returnSearch = ['ActionScript', 'AppleScript', 'Asp'] 

    return returnSearch 

}; 
+0

可能的重複[如何返回來自異步調用的響應?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-調用) – Frxstrem

回答

-1

我想使它回到你心目中的價值的唯一辦法是撥打電話的同步,這意味着只要調用AJAX調用時,你將不得不等待它做好準備(你通常不想要的)。你可以從你的結果這樣調用中調用returnSearch功能:

var googleSuggest = function(returnSearch){ 
    var term = $('#searchInput').val(); 
    var service = { 
    youtube: { client: 'youtube', ds: 'yt' }, 
    books: { client: 'books', ds: 'bo' }, 
    products: { client: 'products-cc', ds: 'sh' }, 
    news: { client: 'news-cc', ds: 'n' }, 
    images: { client: 'img', ds: 'i' }, 
    web: { client: 'hp', ds: '' }, 
    recipes: { client: 'hp', ds: 'r' } 
    }; 

var promise = $.ajax({ 
    url: 'https://clients1.google.com/complete/search', 
    dataType: 'jsonp', 
    data: { 
     q: term, 
     nolabels: 't', 
     client: service.web.client, 
     ds: service.web.ds 
    } 
    }) 

    return promise 
}; 


$(function() { 
    $('#searchInput').autoComplete({ 
    minChars: 1, 
    source: function(term, suggest){ 
     var promise = googleSuggest() 
     returnSearch = function(term, choices) { 
     console.log(choices) 
     var suggestions = []; 
     for (i=0;i<choices.length;i++) 
     if (~choices[i].toLowerCase().indexOf(term)) suggestions.push(choices[i]); 
      suggest(suggestions); 
     } 

     $.when(promise).then(function(data) { 
     term = term.toLowerCase(); 
     var result = []; 
     $.each(data[1], function(item, value) { 
      var stripedValue = value[0].replace(/<[^>]+>/g, ''); 
      result.push(stripedValue); 
     }) 
     returnSearch(term, result) 
     }) 
    } 
    }); 
}) 

另見this stackoverflow post

+1

更好的選擇是使外部函數異步,因爲任何長時間運行的同步操作(例如在服務器上獲取資源)將阻止事件循環並使頁面無響應。雖然提取同步更容易,但由於這個原因通常是不好的做法。 – Frxstrem

+0

完美的工作就像一個魅力!感謝隊友......我需要更多地學習承諾。 @ Frxstream你能否建議你的選擇? – Spyder

+1

@Spyder看看我在你的問題的評論中鏈接到的問題;它解釋了我在說什麼。 – Frxstrem