Directive: lock double click on a button

Asked

Viewed 669 times

1

Hello,

I have a directive to lock the double click buttons on my system... However, when clicking a button that is "calling" the directive, it does not do its function (e.g., if the button is Submit, Submit does not happen).

I’d like to use it to several buttons with different functions.

Directive

import { Directive, HostListener } from '@angular/core';

@Directive({
    selector: '[blockClicks]'
})
export class NoDblClickMatDirective {

    constructor() { }

    @HostListener('click', ['$event'])
    clickEvent(event) {
        const button = (event.srcElement.disabled === undefined) ? event.srcElement.parentElement : event.srcElement;
        button.setAttribute('disabled', true);
        setTimeout(function () {
            button.removeAttribute('disabled');
        }, 2000);
    }
}

Module Directive

import { NgModule } from "@angular/core";
import { NoDblClickMatDirective } from "./nodblclickmat.directive";

@NgModule({
    imports: [],
    declarations: [NoDblClickMatDirective],
    exports: [NoDblClickMatDirective]
})
export class NoDblClickMatDirectiveModule { }

Html snippet

<button class="btn btn-info" blockClicks>Entrar</button>

  • 2

    I assume you tested that on Google Chrome. I’ve never done background research, but there are browsers that don’t submit the form if you set the button as disabled. If I’m not mistaken it happened a while ago and in Firefox worked normally, Chrome blocked.

  • Hmmm, @Andersoncarloswoss, I will test on other browsers... If that’s the case, I would solve how?

  • @Andersoncarloswoss You’re right, in Firefox it worked...

  • I hear this a lot :D Usually you can get around it by disabling the button in the event submit form, but in this case maybe you can check if it’s a button like submit and manually force commit.

  • You think disable setar to readonly can solve?

  • A button can’t be readonly.

  • Oops, I gaffed kkk

  • Can you help me a little more on how I would do the event? I had done in the function of Submit, but as I will use in several buttons, I wanted to do a thing

Show 3 more comments

1 answer

1


I would do something like this:

import { Directive, HostListener } from '@angular/core';

@Directive({
    selector: '[blockClicks]'
})
export class NoDblClickMatDirective {
    lastClick = 0;

    constructor() { }

    @HostListener('click', ['$event'])
    clickEvent(event) {    
        const currentTime = new Date().getTime();
        const ultimoClick = currentTime - this.lastClick;


        if (ultimoClick < 300) {//voce pode adaptar de acordo com o que vc considera um click duplo
            event.stopPropagation();
        } 
        this.lastClick = currentTime;
    }
}
  • Is giving this message on this.ultimoClick = currentTime > 'ultimoClick' property does not exist in type 'Nodblclickmatdirective'.

  • Can you explain to me what you thought, I never made directive so I do not understand very well the orders... the 300 is the amount of clicks? stopPropagation it prevents the button function? q I would consider a double click for example?

  • I did almost what you ordered, but I did it, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.