2016-05-12 9 views
0

のためのsetInterval()の作業を行いますI以下の機能があります。は、1秒のループ

function successFunction(position) { 
      var lat = position.coords.latitude; 
      var lng = position.coords.longitude; 
      var url= '<?php echo url("/") ?>/provider/location/set?lat='+lat+'&lng='+lng; 
      console.log(url); 
      $.ajax({ 
        type: 'get', 
        url: '<?php echo url("/") ?>/provider/location/set?lat='+lat+'&lng='+lng, 
        success: function (msg) { 
        console.log("Location captured"); 
        }, 
        processData: false, 
       });     
     } 

何それがないことはそれが特定のページ(タブ)がオープンしているユーザーのカレンgeolocalizationを送信されているが。これは全体のスクリプトです:

<script type="text/javascript"> 
     if (navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(successFunction); 
     } else { 
      alert('Parece que la Geolocalización, la cual se requiere para esta página, no está habilitado en su navegador. Por favor, utilice un navegador que lo soporta o active la opción.'); 
     } 
     function successFunction(position) { 
      var lat = position.coords.latitude; 
      var lng = position.coords.longitude; 
      var url= '<?php echo url("/") ?>/provider/location/set?lat='+lat+'&lng='+lng; 
      console.log(url); 
      $.ajax({ 
        type: 'get', 
        url: '<?php echo url("/") ?>/provider/location/set?lat='+lat+'&lng='+lng, 
        success: function (msg) { 
        console.log("Location captured"); 
        }, 
        processData: false, 
       });     
     } 

     </script> 

悪いことは、ユーザーがページをリフレッシュし続ける場合にのみ送信することです。私はそれを1秒ごとにループさせて、GEOがリアルタイムで送信し、ユーザーを追跡できるようにしようとしています(これは車geolocalization用です)。リアルタイムで情報を送信する方法はありますか、どうすればSetIntervalを動作させることができますか?これは私の持っているものですが、動作しません。

window.setInterval(successFunction, 1000); 
+0

setIntervalの呼び出しでpositionパラメータが渡されていないため、コードの冒頭にあるように呼び出す必要があります(回答の説明のように) – IrkenInvader

答えて

3

この内容はどうですか?

setInterval(function(){ 
    navigator.geolocation.getCurrentPosition(successFunction); 
}, 1000); 
1

実際の関数をいくつかのパラメータで呼び出すために任意のコールバックを追加する必要があります。

if (navigator.geolocation) { 
     window.setInterval(function(){ 
      navigator.geolocation.getCurrentPosition(successFunction); 
     }, 1000); 

    } else { 
     alert('Parece que la Geolocalización, la cual se requiere para esta página, no está habilitado en su navegador. Por favor, utilice un navegador que lo soporta o active la opción.'); 
    } 
    function successFunction(position) { 
     var lat = position.coords.latitude; 
     var lng = position.coords.longitude; 
     var url= '<?php echo url("/") ?>/provider/location/set?lat='+lat+'&lng='+lng; 
     console.log(url); 
     $.ajax({ 
       type: 'get', 
       url: '<?php echo url("/") ?>/provider/location/set?lat='+lat+'&lng='+lng, 
       success: function (msg) { 
       console.log("Location captured"); 
       }, 
       processData: false, 
      });     
    } 
関連する問題