在http请求中发送带有持续时间的数据

发布于 2024-12-20 11:24:41 字数 446 浏览 1 评论 0原文

您好,这是我的 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);
}

Hi this is my js code for sent data to server using http request. code working fine and i can get data but i want sent data for every 10mins automatically once i call this function. Can any one help me.thanks in advance

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱纠缠 2024-12-27 11:24:41

从这个问题中准确说出你想要什么有点困难,但是你在寻找这样的东西吗?

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

It's a little difficult to tell exactly what you want from the question, but are you looking for something like this?

// 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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文