如何避免出在浏览器中的内存错误由于太多的AJAX调用 [英] How to avoid out of memory error in a browser due to too many ajax calls
问题描述
我建设,需要做出28000数据库调用使用Web应用程序jQuery的AJAX的简易模式的一次。
I'm building a web application that needs to make about 28000 database calls using the jquery ajax shortform all at once.
它得到通过关于精的话费6000,但随后的浏览器给我讲了下面的错误(每个呼叫)在浏览器控制台的20000:
It gets through about 6000 of the calls fine, but then the browser gives me about 20000 of the following error (one for each call) in the browser console:
POST(我的数据库调用)网:ERR_INSUFFICIENT_RESOURCES
POST (my database call) net : : ERR_INSUFFICIENT_RESOURCES
有谁知道如何解决这个问题?也许创建一个缓冲区或东西吗?
Does anyone know how to get around this? Maybe to create a buffer or something?
谢谢!
编辑1:增加了一些code:
edit 1 : adding some code:
戒律,因此用户将填补一些值(比如GHI> 4500,157.5和202.5之间的方面)
Aright, so the user would fill in some values (say GHI > 4500, aspect between 157.5 and 202.5)
下面的通话将被制成:
loadAllData('ghi', 4500, findIdealPoints);
这导致调用此函数:
function loadAllData(type, above, callback){
var data = {};
$.post('php/getIdealData.php?action=get&type='+type+'&above='+above, data, callback);
}
它运行此查询在PHP中:
which runs this query in PHP:
"SELECT `GHI` , `lat` , `long`
FROM solar
WHERE `GHI` >'{$_GET['above']}' ORDER BY `lat`,`long`;";
这在JSON格式的数组返回约28880的记录,并调用执行以下操作回调方法:
That returns about 28880 records in an array in JSON format and calls the callback method which does the following:
function findIdealPoints(data){
var i = 0;
while (i < data.length){
loadAspectWithinRange('aspect', data[i]['lat'], data[i]['long'], 10, compareWithAspect);
i++;
}
}
它运行这个PHP查询:
Which runs this php query:
"SELECT `aspect`,
`lat`, `long`, distance_in_km
FROM (
SELECT `aspect`, `lat`, `long`,r,
(6378.10 * ACOS(COS(RADIANS(latpoint))
* COS(RADIANS(`lat`))
* COS(RADIANS(longpoint) - RADIANS(`long`))
+ SIN(RADIANS(latpoint))
* SIN(RADIANS(`lat`)))) AS distance_in_km
FROM aspect
JOIN (
SELECT '{$_GET['lat']}' AS latpoint, '{$_GET['long']}' AS longpoint, 10.0 AS r
) AS p
WHERE `lat`
BETWEEN latpoint - (r / 111.045)
AND latpoint + (r / 111.045)
AND `long`
BETWEEN longpoint - (r / (111.045 * COS(RADIANS(latpoint))))
AND longpoint + (r / (111.045 * COS(RADIANS(latpoint))))
AND `aspect`
BETWEEN '{$_GET['lowA']}' AND '{$_GET['highA']}'
) d
WHERE distance_in_km <= r
ORDER BY distance_in_km";
和去回调函数运行这样的:
and goes to the callback function that runs this:
function compareWithAspect(data){
var idealPoints =[];
for (var i=0; i<data.length; i++){
idealPoints.push(new google.maps.LatLng(data[i]['lat'], data[i]['long']));
}
if (idealPoints.length > 1){
makePolygon(idealPoints)
}
}
和makePolygon刚刚绘制使用谷歌地图API在地图上。
and makePolygon just draws on the map using the Google Maps API.
我知道这是一个很多,似乎令人费解,我喜欢它,如果任何人都可以给我一个更好的方式来做到这一点!
I know it's a lot and seems convoluted, I would love it if anyone could show me a better way to do this!
再次感谢
推荐答案
您可以做这样的事情。
function findIdealPoints(data){
var i = 0;
while (i < data.length){
loadAspectWithinRange('aspect', data[i]['lat'], data[i]['long'], 10,
compareWithAspect);
i++;
}
而不是做一个Ajax调用,每次出现的发送数据对象到您的通话
Instead of doing an Ajax call for each occurrence send the data object to your call
loadAspectWithinRange('aspect',data,10,compareWithAspect)
然后在Ajax请求对象发送到你的服务的阵列和一个检索结果所有的人而不是一个。
Then in the Ajax request send the array of objects to your service and retrieve the results for all of them instead of one by one.
$.ajax({
url:"...",
data:{
attr1:'aspect',
points: data(here is the array retrieved from "getIdealData.php")
attr2: 10
},
success:function(data){
compareWithAspect(data)
}
})
在服务器端处理构建对象的数组,在 getIdealData.php
点的所有元素。
In the server side processing build an array of the objects for all the element on the getIdealData.php
points.
这将是更好的,而不是做一个Ajax为每个元素
This will be better instead of doing an Ajax for each element
这篇关于如何避免出在浏览器中的内存错误由于太多的AJAX调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!