使用javascript定期更新文本框

时间:2015-09-10 20:08:31

标签: javascript html

我有一个方法getSpeed()定期计算两个LatLng点之间的行进速度。目前我只是将结果打印到控制台,想知道如何将其输出到我的网页上的文本框/提要中?

//retrieve information from database
     downloadUrl("Map.php", function(data){
          var xml = data.responseXML;
          var allPoints = Array();
          var markers = xml.documentElement.getElementsByTagName("marker");

          for(var i = 0; i<markers.length; i++){
            var point = new google.maps.LatLng(
                    parseFloat(markers[i].getAttribute("lat")),
                    parseFloat(markers[i].getAttribute("lng")));
                    allPoints[i] = point;
                }

            for(var i = 1; i <allPoints.length; i++){
                var distance = google.maps.geometry.spherical.computeDistanceBetween(
                        allPoints[i], allPoints[i-1]);
                var speed = ((distance/1000)*60);
                        console.log(distance.toFixed(2)+'m at a speed of '+speed.toFixed(2)+'km/h');

                        if(speed < 8){
                            console.log('WARNING: SPEED IS LOW!');
                        }
           }


});
}

我还有一个div定义如下,我希望将其用作我的文本框...

   <div id="log" style="display: inline-block; margin-top: 100px; border: solid; border-color: black; border-width: 1px; width: 500px; height: 500px;" ></div>   

1 个答案:

答案 0 :(得分:0)

可以这么简单:

document.getElementById('log').innerHTML = distance.toFixed(2) + 
   'm at a speed of ' + speed.toFixed(2) + 'km/h';

if(speed < 8){
  document.getElementById('log').innerHTML += '<br />WARNING: SPEED IS LOW!';

我们正在div查找id,并替换其内容。