我正在尝试在请求完成后隐藏ajax加载器,但在发出请求之前,在blur()事件之后立即触发done()回调.我让我的控制器动作睡了5秒,以确保是这种情况,而且确实如此.我认为只有在结果从服务器
$('#order_billing_post_code').on 'blur', ->
field = $(this)
post_code = field.val()
type = field.data 'address-type'
if post_code.length is 8
xhr = $.ajax
url: "/address_lookups/new"
data:
post_code: post_code
beforeSend: ->
field.siblings('i.address-ajax-loader').show()
success: (data) ->
parse data, type
dataType: "json"
xhr.done(
alert "done"
)
我不熟悉这种语法(Coffeescript?)但看起来你将调用alert“done”的结果立即传递给xhr.done(),而不是将引用传递给调用alert的函数.
尝试:
xhr.done -> alert "done"
在vanilla JS中,就像你写的一样:
xhr.done(alert("done"))
代替:
xhr.done(function() {
alert("done");
});
警报调用立即发生,结果传递给xhr.done,然后根本没有任何用处.
