-1

我有 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]);

} 
4

2 回答 2

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

这个例子

记得避免 document.write

于 2013-05-28T13:10:13.330 回答
0

这只是全局和局部方差的问题。当已经var map = null在全局中定义时,请避免在函数中定义 var map。在函数中没有“var”,只有“map”就足够了。

于 2013-06-10T15:29:22.790 回答