我有5个信息同时出现,如“long”,“lat”,“speed”,“immat”和“date”。首先,只要信息仍然可用,每0.1秒新的5个信息进入,然后每次有另一组新的5信息可用,另一轮相同的事情发生。我想知道如何临时保留所有这些值并显示它。
var myPin = Table_Pins[immat];
myPin.Lat[i] = document.getElementById("Lat");
myPin.Long[i] = document.getElementById("Long");
myPin.immat[i] = document.getElementById("immat");
myPin.date[i] = document.getElementById("date");
myPin.speed[i] = document.getElementById("vitesse");
for (i=0; i<myPin.length; i++){
document.write("lat=" +myPin.Lat[i]);
document.write("long=" +myPin.Long[i]);
document.write("immat=" +myPin.immat[i]);
document.write("date=" +myPin.date[i]);
document.write("speed=" +myPin.speed[i]);
}
答案 0 :(得分:1)
使用Object
将数据存储在Array
:
var pins = []; //this will contain a list of your objects, it's better than keeping an object containing arrays
//inside your function that stores data you will have this
var pin = {
lat: document.getElementById('Lat'),
long: document.getElementById('Long'),
immat: document.getElementById('immat'),
date: document.getElementById('date'),
speed: document.getElementById('vitesse')
};
pins.push(pin);
//and after pushing it you could add to DOM dynamically without looping over al the pins
假设你有一个<ul>
id="pins"
,你可以做类似的事情(就在pins.push(pin)
之后):
var pinUl = document.getElementById('pins'); //you can declare this after pins
//and now the code that adds an element to the HTML page
var pinLi = "<li>" + JSON.stringify(pin) + "</li>";
pinUl.innerHTML += pinLi; //append the pin at the end of the other
请参阅this example
请记住避免 document.write
!
答案 1 :(得分:0)
这只是全局和局部方差的问题。在全局中已定义var map = null
时,避免在函数中定义var map。在函数中只有“map”就足够了,没有“var”。