Preventing new clicks that happen within 3 secs of last click


#1

I am trying to prevent clicks that happen within 3 secs of the last click. Currently my onClicked() function isn’t working properly. What is wrong with my logic?

html

<ion-content (tap)=onClicked($event)>

</ion-content>

component

onClicked(event) {

    let newClick = new Date().getTime();
    let oldClick = 0;

    if((newClick - oldClick) < 3000) {
      oldClick = newClick;
      return false; 
    } 
    else {
      this.doSomething();
      oldClick = newClick;
    }

#2
/*You must use oldClick as CLASS VARIABLE, define it before constructor function.
export class MYCLASS {
    private oldClick : number = 0;
    constructor(public navCtrl: NavController,.......
*/
onClicked(event) {

    let newClick = new Date().getTime();

    if ((newClick - this.oldClick) < 3000) {
        //Do a toast if not enough time from last clicked
        return false;
    } else {
        this.doSomething();
        this.oldClick = newClick;
    }
}