如何避免因ajax调用过多导致浏览器出现内存不足错误(How to avoid out of memory error in a browser due to too many ajax calls)

编程入门 行业动态 更新时间:2024-10-24 21:34:53
如何避免因ajax调用过多导致浏览器出现内存不足错误(How to avoid out of memory error in a browser due to too many ajax calls)

我正在构建一个Web应用程序,需要同时使用jquery ajax shortform进行大约28000个数据库调用。

它通过大约6000次调用,但是浏览器在浏览器控制台中给出了大约20000个以下错误(每个调用一个):

POST(我的数据库调用)net :: ERR_INSUFFICIENT_RESOURCES

有谁知道怎么解决这个问题? 也许创建一个缓冲区或什么?

谢谢!

编辑1:添加一些代码:

正确,所以用户会填写一些值(比如GHI> 4500,方面介于157.5和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中运行此查询:

"SELECT `GHI` , `lat` , `long` FROM solar WHERE `GHI` >'{$_GET['above']}' ORDER BY `lat`,`long`;";

它以JSON格式返回一个数组中的大约28880条记录,并调用回调方法,该方法执行以下操作:

function findIdealPoints(data){ var i = 0; while (i < data.length){ loadAspectWithinRange('aspect', data[i]['lat'], data[i]['long'], 10, compareWithAspect); i++; }

}

哪个运行这个php查询:

"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";

并转到运行此的回调函数:

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只是使用Google Maps API在地图上绘制。

我知道它很多而且看起来很复杂,如果有人能告诉我更好的方法,我会很高兴!

再次感谢

I'm building a web application that needs to make about 28000 database calls using the jquery ajax shortform all at once.

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 (my database call) net : : ERR_INSUFFICIENT_RESOURCES

Does anyone know how to get around this? Maybe to create a buffer or something?

Thanks!

edit 1 : adding some code:

Aright, so the user would fill in some values (say GHI > 4500, aspect between 157.5 and 202.5)

The following call would be made:

loadAllData('ghi', 4500, findIdealPoints);

This call leads to this function:

function loadAllData(type, above, callback){ var data = {}; $.post('php/getIdealData.php?action=get&type='+type+'&above='+above, data, callback);

}

which runs this query in PHP:

"SELECT `GHI` , `lat` , `long` FROM solar WHERE `GHI` >'{$_GET['above']}' ORDER BY `lat`,`long`;";

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++; }

}

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) } }

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!

Thanks again

最满意答案

你可以这样做。

function findIdealPoints(data){ var i = 0; while (i < data.length){ loadAspectWithinRange('aspect', data[i]['lat'], data[i]['long'], 10, compareWithAspect); i++; }

而不是为每次事件执行Ajax调用,而是将数据对象发送到您的调用

loadAspectWithinRange('aspect',data,10,compareWithAspect)

然后在Ajax请求中将对象数组发送到您的服务并检索所有这些对象的结果,而不是逐个检索。

$.ajax({ url:"...", data:{ attr1:'aspect', points: data(here is the array retrieved from "getIdealData.php") attr2: 10 }, success:function(data){ compareWithAspect(data) } })

在服务器端处理中,为getIdealData.php点上的所有元素构建一个对象数组。

这将更好,而不是为每个元素做一个Ajax

You could do something like this.

function findIdealPoints(data){ var i = 0; while (i < data.length){ loadAspectWithinRange('aspect', data[i]['lat'], data[i]['long'], 10, compareWithAspect); i++; }

Instead of doing an Ajax call for each occurrence send the data object to your call

loadAspectWithinRange('aspect',data,10,compareWithAspect)

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) } })

In the server side processing build an array of the objects for all the element on the getIdealData.php points.

This will be better instead of doing an Ajax for each element

更多推荐

本文发布于:2023-08-07 20:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466008.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:浏览器   错误   内存不足   avoid   ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!