首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何安排延迟5秒发送ajax请求?

如何安排延迟5秒发送ajax请求?
EN

Stack Overflow用户
提问于 2018-06-02 11:19:05
回答 1查看 33关注 0票数 0

按下按钮后,我想等待5秒,然后发送ajax请求。

现在,只要按下按钮,请求就会在一秒钟内发送。

代码语言:javascript
复制
<p id="test" rows="7" class="combo">
azmir:azmir
</p>
<button id="jsonp2" type="button" data-bind='click: registerClick'>Start</button>
<div class="aprovadas"></div>
代码语言:javascript
复制
$(document).ready(function () {
    $('.combo').each(function () {
        var input = '#' + this.id;
        var count = 0;
        $(count).show();
        combo(input, count);
        $(this).keyup(function () {
            combo(input, count)
        });
    });
});

function combo(field, count) {
    var number = 0;
    var matches = $(field).val().match(/(.+)/gm);

    if (matches) {
        number = matches.length / 1;
    }

    $(count).text(number + ' ');
}

$(document).ready(function () {
    $("#jsonp2").click(function () {
        var number = 0;
        var username = "";
        var password = "";

        $.ajax({
            url: "http://***",
            success: function (response) {
                var data = response.user_info;
                if (data.status.match("Active")) {
                    $(".aprovadas").append("My account work");
                }
            }
        });
    });
});

http://jsfiddle.net/qm3do4uu/552/

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-02 11:24:08

试试这个剧本。

代码语言:javascript
复制
$(document).ready(function() {
    $('.combo').each(function() {
    var input = '#' + this.id;
    var count = 0;
    $(count).show();
    combo(input, count);
    $(this).keyup(function() {
        combo(input, count)
    });
    });
});

function combo(field, count) {
    var number = 0;
    var matches = $(field).val().match(/(.+)/gm);
    if (matches) {
    number = matches.length / 1;
    }
    $(count).text(number + ' ');
}

$(document).ready(function() {
    $("#jsonp2").click(function() {
    var number = 0;
    var username = "";
    var password = "";
    setTimeout(function() {
        $.ajax({
        url: "http://***",
        success: function(response) {
            var data = response.user_info;
            if (data.status.match("Active")) {
            $(".aprovadas").append("My account work");
            }
        }
        });
    }, 5000);
    });
});

在ajax调用之前,您应该使用setTimeout(function() { }, 5000);

琴杆

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50656375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档