[英]How to minimize ajax calls functions
我有一個具有ajax調用的函數,並且兩個函數中的數據相同。 Ajax和函數可以正常工作,但是我在ajax中有幾個返回很多值的函數。 我想知道有沒有一種方法可以使用ajax函數,並且可以在ajax中調用某些函數?
所以我不會做點什么:
這個ajax有一個函數調用handleData
start = function(rowsInDB, ratio_Over_rows, lastx_gp){
//ajax ONLY calls don't return anything
(function($) {
//Connects to the json file
var url = 'XXX?';
//Automatic refresh
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'jsonCallback',
contentType: "application/json",
dataType: 'jsonp',
success: function(data) {
handleData(data, 7, 5, 5);
},
error: function(e) {
console.log(e.message);
}
});
})(jQuery);
}
另一個ajax具有不同的功能,稱為placeData,從中讀取相同的數據
plays = function(rowsInDB, ratio_Over_rows, lastx_gp){
//ajax ONLY calls don't return anything
(function($) {
//Connects to the json file
var url = 'http://beniky.co.uk/football/ast.json?callback=?';
//Automatic refresh
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'jsonCallback',
contentType: "application/json",
dataType: 'jsonp',
success: function(data) {
placeData(data, 7, 5, 5);
},
error: function(e) {
console.log(e.message);
}
});
})(jQuery);
}
PS。 我不希望兩個功能都在一個ajax中
這樣子怎么樣
common = function (rowsInDB, ratio_Over_rows, lastx_gp, callback) {
//ajax ONLY calls don't return anything
(function ($) {
//Connects to the json file
var url = 'XXX?';
//Automatic refresh
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'jsonCallback',
contentType: "application/json",
dataType: 'jsonp',
success: function (data) {
if (typeof callback == "function") {
callback(data, 7, 5, 5);
}
},
error: function (e) {
console.log(e.message);
}
});
})(jQuery);
}
用法
common(rowsInDB, ratio_Over_rows, lastx_gp, handleData);
common(rowsInDB, ratio_Over_rows, lastx_gp, placeData);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.