在jQuery / Coffeescript中延迟ajax调用 [英] Delaying ajax call in jQuery/Coffeescript

查看:239
本文介绍了在jQuery / Coffeescript中延迟ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一些粗糙的javascript / jquery / coffeescript,在页面的下半部分在DIV中滑动延迟。我在一个rails,所以我也打了一个端点,增加一个视图计数器在数据库中的一个特定的幻灯片。

This is some rough javascript/jquery/coffeescript that slides in a DIV on the lower part of the page after a delay. I'm working in a rails, so I'm also hitting an endpoint that increments a view counter in the database for a specific slide.

jQuery ->
    $("div[data-slide='true']").delay(20000).animate({opacity: 1,right:'+=350'},1350, 'swing');
    id = $("div[data-slide='true']").data("slide-id")
    $.ajax({url: "http://localhost:3000/firefly/slides/" + id + "/increment", type: "post"});
    $("div[data-close='true']").click ->
        $("div[data-slide='true']").clearQueue().animate({opacity: 0,right:'-=350'},500, 'easeOutBounce'); 



我知道这不是最好的javascript,我真的是一个新手JS - - 但我现在的问题是我不知道如何延迟ajax调用,所以它只有当幻灯片动画中的点击终点。现在,它达到终点,并增加计数器,当DOM完成加载。

I'm aware this isn't the best javascript, I'm really a novice at JS -- but my problem right now is I can't figure out how to delay the ajax call so it only hits the endpoint when the slide is animated in. Right now, it hits the end point and increments the counter when the DOM has finished loading.

推荐答案

我不知道或使用 coffeescript ),但您可以使用动画的回调。

I don't know or use coffeescript (thanks God), but you can use the callback of the animation.

使用此重载

.animate( properties [, duration] [, easing] [, complete]

将ajax放在完成函数回调中。

Put the ajax in the complete function callback.

这篇关于在jQuery / Coffeescript中延迟ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆