[JQuery / Ajax]降低点击延迟

时间:2017-12-01 20:14:08

标签: jquery ajax laravel ajaxform

我的Ajax功能有点问题:点击按钮寄存器后,我无法降低延迟。

我首先点击提交表单上的按钮,然后等待3秒,然后启动:"登录" - 很少延迟 - 显示信息。

如何使它像点击该按钮一样,直接启动ajax功能:"登录" - 稍微延迟(对于加载图像) - 显示消息?

我的Ajax

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

$(document).on('submit', '#formRegister', function(e) {
    e.preventDefault();

    $('input+small').text('');
    $('input').parent().removeClass('has-error');
    $.ajax({
        method: $(this).attr('method'),
        url: $(this).attr('action'),
        data: $(this).serialize(),
        dataType: "json"
    })
        .done(function(data){
            $('#btn-signup').html('<img src="images/ajax-loader.gif" /> &nbsp; signing in...').prop('disabled', true);
            // Loading gif.
            $('input[type=text],input[type=password]').prop('disabled', true);
            // Disable the input text and password

            setTimeout(function(){

                $('#error_register').slideDown('fast', function(){
                    $messageRegister = "The message";
                    $('#error_register').html('<div class="alert alert-info">'+$messageRegister+'</div>');
                    // Show the message from the variable.
                    $("#formRegister").trigger('reset');
                    // Reset the form
                    $('input[type=email],input[type=password]').prop('disabled', false);
                    // Enable the input text, email, password
                    $('#btn-signup').html('Submit').prop('disabled', false);
                }).delay(3000).slideUp('fast');
                // Hide the div that contain the message.
                window.setTimeout(function(){location.reload()},4500);
                // Reload page after 4.5 seconds.

            },3000);

        })
        .fail(function(data) {
            $.each(data.responseJSON, function (key, value) {
                var input = '#formRegister input[name=' + key + ']';
                $(input + '+small').text(value);
                $(input).parent().addClass('has-error');
                $('#btn-signup').html('Submit').prop('disabled', false);
            });
        });
});

})

Html注册表格

<div class="modal fade" id="myRegister" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
            <h4 class="modal-title" id="myModalLabel">Register</h4>
        </div>
        <div class="modal-body">

            <!-- json response will be here -->
            <div id="error_register"></div>
            <br>
            <!-- json response will be here -->

            <form id="formRegister" class="form-horizontal" role="form" method="POST" action="{{ url('register') }}">
                <input type="hidden" name="_token" value="{{ csrf_token() }}">

                <div class="form-group">
                    <label class="col-md-4 control-label">Name</label>
                    <div class="col-md-6">
                        <input type="text" class="form-control" name="name">
                        <small class="help-block"></small>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-4 control-label">E-Mail Address</label>
                    <div class="col-md-6">
                        <input type="email" class="form-control" name="email">
                        <small class="help-block"></small>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-4 control-label">Password</label>
                    <div class="col-md-6">
                        <input type="password" class="form-control" name="password">
                        <small class="help-block"></small>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-4 control-label">Confirm Password</label>
                    <div class="col-md-6">
                        <input type="password" class="form-control" name="password_confirmation">
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-md-6 col-md-offset-4">
                        <button id="btn-signup" type="submit" class="btn btn-primary">
                            Register
                        </button>
                    </div>
                </div>
            </form>

        </div>
    </div>
</div>

功能寄存器

    /**
 * Handle a registration request for the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function register(Request $request)
{
    $validator = $this->validator($request->all());

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
    }

    $user = $this->create($request->all());
    $user->confirmation_code = str_random(30);
    $user->save();

    event(new Registered($user));

    $this->notifyUser($user);

    return response()->json();
}

0 个答案:

没有答案
相关问题