[英]How to read & parse file using java script?
Here is my code I am reading a file Which contains latitude & longitude , I am sotring the file content in an Array .But I don't want to store it array ,Is there any other options so that I can store & process it. 这是我的代码,我正在读取一个包含纬度和经度的文件,正在将文件内容存储在一个Array中。但是我不想将其存储在array中,是否还有其他选项可以存储和处理它。
I am generating a map with these lat & lon in a for loop,I am passing these to a function,In an array you can not give a delay so I want other method 我在for循环中使用这些纬度和经度生成地图,将它们传递给函数,在数组中您不能给出延迟,所以我想要其他方法
Here is My code : 这是我的代码:
function FileHelper()
{}
{
FileHelper.readStringFromFileAtPath = function(pathOfFileToReadFrom)
{
var request = new XMLHttpRequest();
request.open("GET", pathOfFileToReadFrom, false);
request.send(null);
var returnValue = request.responseText;
return returnValue;
}
}
var pathOfFileToRead = "LatLon.txt";
var contentsOfFileAsString = FileHelper.readStringFromFileAtPath
(
pathOfFileToRead
);
**var contentsArray=new Array();
var contentsArray = contentsOfFileAsString.split('\n');**
function main()
{
for(i=0;i<contentsArray.length;i++)
{
addMarker();
// setTimeout(function() { remveh1();}, 2000);
}
}
function addMarker()
{
console.log(contentsArray[i]);
split_contentArray=contentsArray[i].split(',');
vehicle_lat=split_contentArray[0];
vehicle_lng=split_contentArray[1];
var vehicle = new MQA.Poi({ lat: **vehicle_lat**, lng: **vehicle_lng** });
var icon = new MQA.Icon('https://cdn2.iconfinder.com/data/icons/gpsmapicons/blue/gpsmapicons07.png', 26, 26);
vehicle.setIcon(icon);
vehicle.setKey("abc");
map.addShape(vehicle);
vehicle.setRolloverContent("Vehicle # KA05 9999");
}
function remveh1()
{
map.removeShape(map.removeShape(map.getByKey("abc")));
};
Actually what I want is, I want a delay b/w displaying position on map.
实际上,我想要的是延迟黑白显示在地图上的位置。 From the above code I am displaying all the position at one time, I want to display them one by one, how should i do that
从上面的代码中,我一次显示所有位置,我要一一显示,我该怎么做
By way of (simplified) background, the browser uses the same thread to run JavaScript as it does to repaint, so if you try to update the page within a for
loop the entire loop will complete before the actual repaint. 通过(简化)背景,浏览器使用与重绘相同的线程来运行JavaScript,因此,如果您尝试在
for
循环中更新页面for
则整个循环将在实际重绘之前完成。 You would deal with this by replacing the for
with an algorithm based on setTimeout()
calls. 您可以通过基于
setTimeout()
调用的算法替换for
来解决此问题。 In between timeouts JS is "inactive" (not intended as a technical term) so the browser can repaint. 在两次超时之间,JS是“不活动的”(不是专门的技术术语),因此浏览器可以重新绘制。
Your code includes calls to functions that you don't show, so I can only suggest a fairly minimal update to it, changing addMarker()
to accept a reference to a marker as an argument rather than relying on global contentsArray()
and i
variables, and then rewriting main()
to pass the appropriate marker object when it calls addMarker()
: 您的代码包含对您未显示的函数的调用,因此我只能建议对其进行相当少量的更新,将
addMarker()
更改为接受对标记的引用作为参数,而不是依赖于global contentsArray()
和i
变量,然后重写main()
以在调用addMarker()
时传递适当的标记对象:
function main() {
var i = -1;
(function processNext() {
if (++i < contentsArray.length) { // if there are markers left
addMarker(contentsArray[i]); // add the next one
setTimeout(function() { // then delay before
remveh1(); // removing it and then
processNext(); // process the next one
}, 2000);
}
})();
}
function addMarker(marker)
{
console.log(marker);
split_contentArray = marker.split(',');
// rest of your function here unchanged
}
(Simplified) demo: http://jsfiddle.net/3ao3wved/ (简体)演示: http : //jsfiddle.net/3ao3wved/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.