2011-12-09 4 views
0

こんにちは、これはhttpリクエストを使用してサーバーに送信されたデータのjsコードです。コードはうまく動作し、私はデータを取得することができますが、私はこの関数を呼び出すと自動的に10分ごとにデータを送信したい。事前にいずれかのヘルプme.thankshttpリクエストで時間のあるデータを送信

xmlHttp=new XMLHttpRequest(); 
var url="http://localhost"; 
xmlHttp.open("POST",url,true); 
var params = "lorem=ipsum&name=binny"; 
function timerMethod() 
{ 
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
xmlHttp.setRequestHeader("Content-length", params.length); 
xmlHttp.send(params); 
} 
+0

サーバに過去10分間のデータを返させる必要があります。代わりに、代わりにサーバー側のコードを表示できますか? – Martin

答えて

0

できそれはあなたが質問から望むまさに言うには少し難しいですが、あなたはこのような何かを探していますか?

// Declare variables 
var timedUpdate, getRequestParams, url, updateTimeout; 

// Define timedUpdate function 
timedUpdate = function() { 

    // Declare variables 
    var xmlHttp, params; 

    // Create a new AJAX object 
    xmlHttp = new XMLHttpRequest(); 

    // Define a call back function 
    xmlHttp.onreadystatechange = function() { 

     if (xmlHttp.readyState < 4) { 
      return; // Only do something if the request if complete 
     } 

     if (xmlHttp.responseCode != 200) { 
      // Handle HTTP errors here 
      // e.g. 
      alert('Something went wrong with the AJAX request (HTTP '+xmlHttp.responseCode+')'); 
     } 

     // Do your thing with the returned data 

     // Set the function to run again after updateTimeout seconds 
     setTimeout(timedUpdate, updateTimeout * 1000); 
    }; 

    // Get the request parameters 
    params = getRequestParams(); 

    // Send the request 
    xmlHttp.open("POST", url, true); 
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
    xmlHttp.setRequestHeader("Content-length", params.length); 
    xmlHttp.send(params); 

}; 

// Define getRequestParams function 
getRequestParams = function() { 
    // This function returns a parameter string to be used in the request 
    // I am guessing you need to generate a new one every 10 minutes 
    return "lorem=ipsum&name=binny"; 
}; 

// Define the URL to be used in the requests 
url = "http://localhost/"; 

// Define how often the function is repeated, in seconds 
updateTimeout = 600; // 10 minutes 

// Make the first call to the function 
timedUpdate(); 
関連する問題