我在我的网站上使用 Google Maps API。我显示一条路线、距离和路线的持续时间。因此我有以下代码:
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,});
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer({
draggable: false,
map: map,
panel: document.getElementById('right-panel')});
directionsDisplay.addListener('directions_changed', function() {
computeTotalDistance(directionsDisplay.getDirections());
});
displayRoute('Zeiskamer Straße 1, 76756 Bellheim', 'Voelklingen', directionsService, directionsDisplay);
}
function displayRoute(origin, destination, service, display) {
service.route({
origin: origin,
destination: destination,
travelMode: 'DRIVING',
avoidTolls: true,
drivingOptions: {
departureTime: new Date(Date.now() + 100),
trafficModel: 'optimistic'
},
},
function(response, status) {
if (status === 'OK') {
display.setDirections(response);
} else {
alert('Could not display directions due to: ' + status);
}
});
}
function computeTotalDistance(result) {
var total = 0;
var myroute = result.routes[0];
total = "Die Entfernung beträgt: <b>" +myroute.legs[0].distance.text +"</b>";
document.getElementById('total').innerHTML = total + "<br>"+ "Die voraussichtliche Fahrzeit beträgt: <b>" + myroute.legs[0].duration_in_traffic.text + "</b>" + seconds;
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=MYKEY&callback=initMap">
</script>
我希望duration_in_traffic
每 10 分钟更新一次。但我不知道如何每 10 分钟执行一个带有参数的函数。
我怎样才能做到这一点?
最佳答案
使用 setInterval 以重复的时间间隔执行任务
setInterval( function() { showAlert('this'); }, 10*60*1000 );
function showAlert(text){
alert("called "+text)
}
关于javascript - 如何在 HTML 中每 10 分钟执行一次 Javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44303366/