简体   繁体   English

短暂延迟后如何触发点击

[英]How to trigger click after a short delay

I am building a Backbone App, my view is like this 我正在构建Backbone App,我的看法是这样的

define(function (require) {

    'use strict';

    var MyBackbone      = require('mybackbone'),
        template        = require('utils/template'),
        Handlebars      = require('handlebars'),
        Contest          = require('controllers/ads/contest');

    var Main = MyBackbone.View.extend({

        initialize : function( options ) {
            MyBackbone.View.prototype.initialize.apply(this, [options]);
            this.template = Handlebars.compile( template.get( "presentation/page/main" ) );
            this.loadChildViews( "footer", new Contest() );
        },

        render: function(){
            var renderedContent = this.template();
            this.$el.html( renderedContent );
            return this;
        },

        events: {
            "click .show-message": "hideAndShowMessage"
        },

        hideAndShowMessage: function(e){
            var self = this;
            e.preventDefault();
            this.$el.find('.is-hidding:visible').addClass('hideFade');
            this.$el.find('.bg-lets-go').removeClass('hideFade');
            var targetLink = $(e.currentTarget);
            targetLink.removeClass('show-message');
            setTimeout(function(){
                $('a.no-style:not(".show-message")', self.$el).trigger("click");
                //$('a.no-style:not(".show-message")').click();
                //targetLink.trigger('click');
            }, 750);
        }
    });

    return Main;
});

and my main.jade 和我的main.jade

div.main-container
    .container-fluid.is-hidding
        .row
            .col-xs-6
                a.no-style.btn-ipurpose.show-message(href="/#ipurpose")
                    i.fa.fa-tree.fa-3x
                    | JE PROPOSE

            .col-xs-6
                a.no-style.btn-ichoose.show-message(href="/#ichoose")
                    i.fa.fa-flag-checkered.fa-3x
                    | JE CHOISIS

.bg.bg-lets-go.is-hidding.hideFade
.bg.bg-simple

the goal is to display a "let's go message" for 750ms after the link is clicked. 目标是在点击链接后的750毫秒内显示“放手消息”。

I have tried few stuff into the setTimeout but none of them work. 我在setTimeout中尝试了很少的东西,但没有一个起作用。 No javascript error, is it the right way to do so ? 没有JavaScript错误,这是正确的方法吗?

It seems to me you are making a mistake in your logic. 在我看来,您在逻辑上犯了一个错误。

You are again sending the click event to the same anchor which was clicked before. 您再次将click事件发送到之前被单击的同一锚。 A listener will still be bound to this element, resulting in hideAndShowMessage to be called again. 侦听器仍将绑定到此元素,从而导致hideAndShowMessage再次被调用。 In that method you call e.preventDefault() again. 在该方法中,您再次调用e.preventDefault()。 You start the timeout after which the click is send and we start all over again; 您开始超时,然后发送点击,然后我们重新开始。 basically an infinite loop. 基本上是一个无限循环。

A quick-fix for your issue would be to make the preventDefault call conditional: 快速解决问题的方法是使preventDefault调用成为条件调用:

var targetLink = $(e.currentTarget);
if(targetLink.hasClass(".show-message")) {
    e.preventDefault();
    //....rest of your logic
}

This should do the trick! 这应该可以解决问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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