i_user
i_user

Reputation: 513

Submitting a particular form every x seconds on a page with ajax

I have three forms on my page that i submit with ajax. There is a particular form named form2 that i want to auto submit very 10 seconds.

$(document).ready(function() {
    $Addr = localStorage.getItem('eaddress');
    $email7 = $('#email7')

    if ($Addr !== null) {
        $('#email7').val($Addr);

    }

    if ($Addr != '') {
        $.ajax({
            type: "POST",
            url: "gotopage.php",
            data: $("#form2").serialize(),
            success: function(data) {
                $('#log_msg2').html(data);
                var result = $.trim(data);
                if (result === "Signing In") {
                    window.location = 'mypage.html';
                }

            }

        });
    }
});

Upvotes: 0

Views: 825

Answers (3)

Manish Shukla
Manish Shukla

Reputation: 1365

Call you ajax function inside setInterval function like:

setInterval(function(){ function }, 3000);

like:

function abc(){
 $Addr = localStorage.getItem('eaddress');
    $email7 = $('#email7')

    if ($Addr !== null) {
        $('#email7').val($Addr);

    }

    if ($Addr != '') {
        $.ajax({
            type: "POST",
            url: "gotopage.php",
            data: $("#form2").serialize(),
            success: function(data) {
                $('#log_msg2').html(data);
                var result = $.trim(data);
                if (result === "Signing In") {
                    window.location = 'mypage.html';
                }

            }

        });
    }
}

Then call function like

setInterval(function(){ abc }, 3000);

Upvotes: 1

Michael Doye
Michael Doye

Reputation: 8171

You can wrap just the ajax in setInterval():

$(document).ready(function() {
    $Addr = localStorage.getItem('eaddress');
    $email7 = $('#email7');

    if ($Addr !== null) {
        $('#email7').val($Addr);
    }
    if ($Addr != '') {
        setInterval(function() {
        $.ajax({
            type: "POST",
            url: "gotopage.php",
            data: $("#form2").serialize(),
            success: function(data) {
                $('#log_msg2').html(data);
                var result = $.trim(data);
                if (result === "Signing In") {
                    window.location = 'mypage.html';
                }

            }

        });

        }, 10000);
    }
});

Upvotes: 0

Deepesh
Deepesh

Reputation: 6398

You can use this for the AJAX request every 10 seconds:

setInterval(function() {
  // Do something every 10 seconds
}, 10000);

Upvotes: 2

Related Questions