отправленные данные с продолжительностью времени в запросе http - PullRequest
0 голосов
/ 09 декабря 2011

Привет, это мой код js для отправки данных на сервер с помощью http-запроса.код работает нормально, и я могу получить данные, но я хочу, чтобы отправленные данные автоматически каждые 10 минут, как только я вызываю эту функцию.Может ли кто-нибудь мне помочь. Спасибо заранее

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);
}

1 Ответ

0 голосов
/ 09 декабря 2011

Сложно сказать точно, чего вы хотите от вопроса, но вы ищете что-то подобное?

// 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();
...