简体   繁体   中英

Repeating a function every x seconds

I am trying to repeat the function "refresh(geocoder, map, infowindow)" every x seconds but it won't work. Below is what the code that i have tried in html and javascript. Any help given is appreciated.

I am trying to repeat the function "refresh(geocoder, map, infowindow)" every x seconds but it won't work. Below is what the code that i have tried in html and javascript. Any help given is appreciated.

Thank you.

 function initMap() { var geocoder = new google.maps.Geocoder; var infowindow = new google.maps.InfoWindow; myAddress = document.getElementById("address"); function refresh(geocoder, map, infowindow) { $.ajax({ type: "GET", url: 'http://localhost/scripts/retrievegeolocation.php', dataType: "json", success: function(data) { document.getElementById("Lat").innerHTML = data.Lat; // show response from the php script. document.getElementById("Lng").innerHTML = data.Lng; var latlng = {lat: parseFloat(data.Lat), lng: parseFloat(data.Lng)}; geocoder.geocode({'location': latlng}, function(results, status) { if (status === 'OK') { if (results[0]) { map.setZoom(15); var marker = new google.maps.Marker({ position: latlng, map: map,icon:'http://localhost/img/car.png', }); infowindow.setContent(results[0].formatted_address); infowindow.open(map, marker); myAddress.innerHTML="Address: " + results[0].formatted_address; } else { window.alert('No results found'); } } else { window.alert('Geocoder failed due to: ' + status); } }); } }); } 
  <br/><center> LAT : <p id="Lat" name="Lat" value=" "></p></center><br/> <center>LNG : </center> <center><p id="Lng" name="Lng" value=" "></p></center> <center><p style="font-size:20px;" id="address"></p></center> <button onclick="function() { refresh(geocoder, map, infowindow); }">Try it</button> 

It appears that you are missing a closing parenthesis for the function initMap()

I have removed all the code inside and called the dummy functions. It is working fine. Below is the code snippet. Copy paste this into browser console to run this.

function initMap() {

    function refresh(geocoder, map, infowindow) {  
        alert(' refresh');  
    }
    setInterval(refresh, 1000);
    }
initMap();

Your button that is calling refresh(geocoder, map, infowindow) doesn't know what geocoder, map, or infowindow are. Two of those items are instantiated inside the initMap() function which is presumably the async loading Google javascript's callback function and map was never instantiated.

Assuming you have an element with the id of "map", put var map = document.getElementById("map"); close to the top of the initMap() function and then try adding this code within the initMap() function after you declare the refresh function.

setInterval( function() {
    refresh(geocoder, map, infowindow);
}, 20 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM