How to set a timer using jQuery to sent HTTP post data of HTML form

China☆狼群 提交于 2019-12-04 15:44:40

This will do:

setInterval(submit_me, 600000); // (1000 * 60 * 10 = 600000)

function submit_me() {

    $('#form').submit();

}

Shorter version:

setTimeout(function() { $('#form').submit(); }, 5000);

Use setTimeout(function, millisDelay)

$("document").ready(function() {            
    setTimeout(function(){
        var checkedInputElements = $(":input[value=true]");
        var paramNames = [];
        jQuery.each(checkedInputElements, function() {
            var paramName = $(this).attr("name");
            paramNames.push(paramName);
        });
        $("form").submit(function() {
            jQuery.post({
              type: 'POST',
              url: "http://localhost:8080/wickedlynotsmart/auto-submit-form",
              data: paramNames
          });
          return true;
        });
    }, 600000);
});  

NB. setTimeout(function, millisDelay) is a built in javascript function - it's not part of jQuery or any other library

With a success handler:

$("document").ready(function() {            
    setTimeout(function(){
        var checkedInputElements = $(":input[value=true]");
        var paramNames = [];
        jQuery.each(checkedInputElements, function() {
            var paramName = $(this).attr("name");
            paramNames.push(paramName);
        });
        $("form").submit(function() {
            jQuery.post({
              type: 'POST',
              url: "http://localhost:8080/wickedlynotsmart/auto-submit-form",
              data: paramNames,
              success: function(data, textStatus, jqXHR) {
                  alert(data); //data contains the response from the servlet you posted to.
                  document.location = "http://Where-I-want-to-redirect-to";
              }
          });
          return true;
        });
    }, 600000);
}); 

Are anythin working at all? Use console.log() or alert() to follow the code.

You´re passing the document as a string.

$("document").ready(function() { ...

should be

$(document).ready(function() { ...

Regarding the timer to submit the form; your example just bind the ajax post to the forms submit event, it doesn´t submit the form or send the ajax request.

Do you want the form to be submited after 10 minutes? Should the user be able to check/uncheck the checkboxes during that time or should the (unchecked) checkboxes be selected and added to paramNames on document ready?

UPDATE

Created a live example;

HTML

<form id="myForm">
    1 <input type="checkbox" name="checkbox1" />
    2 <input type="checkbox" name="checkbox2" />
    3 <input type="checkbox" name="checkbox3" />
    <input type="submit" value="submit form" />
</form>

Javascript

$(document).ready(function() {

    console.log('document ready'); // DEBUG

    setTimeout(function() {

        console.log('setTimeout()'); // DEBUG
        submitForm();

    }, 1000 * 5); // 5 seconds

    $('#myForm').submit(function() {
        console.log('submit event'); // DEBUG
        submitForm();
    });

});

var submitForm = function() {

    console.log('submitForm()'); // DEBUG

    var $checkedInputElements = $('input[type="checkbox"]:checked');
    var paramNames = [];

    $checkedInputElements.each(function() {
        paramNames.push($(this).attr("name"));
    });

    console.log('paramNames =', paramNames); // DEBUG

    jQuery.ajax({
        type: 'POST',
        url: '/echo/json/',
        data: paramNames,
        complete: function(jqXHR, textStatus) {
            console.log('Done: ', textStatus); // DEBUG
            $('input[type="checkbox"]').attr('disabled', true);
        }
    });

    return true;
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!