2012-12-11 66 views
0

嗨我對JavaScript很陌生,我有一個小的Javascript腳本,它可以獲取使用地理位置的lat和lon,然後將它傳遞給php頁面。Javascript函數每隔2分鐘運行一次,不加載其他頁面

我想要做的就是運行功能每隔兩分鐘什麼,但每個功能運行它時加載test.php的頁面

我的代碼如下

function position(){ 
    var a=setTimeout(position(),60000) 
    } 


    if(navigator.geolocation) 
     { 
     navigator.geolocation.getCurrentPosition(function(position) 
     { 
     var lat = position.coords.latitude; 
     var lon = position.coords.longitude; 
     document.location = "locator/test1.php?lat=" + lat + "&lon=" + 
        lon; 

     }); 
    } 

有沒有去傳球變量到php頁面而不加載它?或者什麼是調用javascript函數以每兩分鐘更新一次細節的最佳方式。

謝謝

+1

https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started,如果你使用jQuery:http://api.jquery.com/jQuery.ajax/ – akonsu

回答

2

首先,你在上面的代碼中有一個錯誤,可能是一個錯字。

var a=setTimeout(position(),60000) <-- calling the function, not assigning it. 

應該

var a=setTimeout(position,60000); 

現在回答你的問題:

,從AJAX請求到服務器。

var xmlHttp = new XMLHttpRequest(); //not the cross browser way of doing it 
xmlHttp.open("GET", "locator/test1.php?lat=" + lat + "&lon=" + lon, true); 
xmlHttp.send(null); 
+0

你好謝謝你ajax,解決方案工作完美,但不會在1分鐘後自動更新 –

+0

什麼不更新?這一頁?您需要處理響應並以某種方式處理輸出。 – epascarello

相關問題