简体   繁体   中英

Ajax call after 5 secs

If I click on a button is it possible to call an ajax after 5 secs?

HTML :

<button>Click</button>
<p id='output'></p>

AJAX:

$('button').click(function() {
    $.ajax({
        type: 'post',
        url: 'save_student_db.php',
        data: 's_name=' + $('#s_name').val() +
            '&s_email=' + $('#s_email').val() +
            '&s_phone=' + $('#s_phone').val() +
            '&s_add=' + $('#s_add').val() +
            '&dept_select=' + $('#dept_select').val() +
            '&datepiker=' + $('#datepiker').val(),
        success: function(reply_data) {
            $('#output').html(reply_data);
        }
    });
});

Here I want when I click on the button then this ajax call will called after 5 secs. How can I do it? thank you.

Yes, like this:

$('button').click(function() {
    setTimeout(function () {
        $.ajax({
            type: 'post',
            url: 'save_student_db.php',
            data: 's_name='+ $('#s_name').val() + 
                  '&s_email=' + $('#s_email').val() + 
                  '&s_phone=' + $('#s_phone').val() + 
                  '&s_add=' + $('#s_add').val() + 
                  '&dept_select=' + $('#dept_select').val() + 
                  '&datepiker=' + $('#datepiker').val(),
            success: function(reply_data) { 
                $('#output').html(reply_data);
            },
        });
    }, 5000);
});

You can use setTimeout()

setTimeout(function(){ $(".selectSomething").yourAjaxFunctionHere() }, 5000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM