Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Link for some time after being clicked?

To prevent impatient users from clicking on a link to a webstart application too often, I tried to disable the hyperlink for some seconds after it has been called the first time.

<a href="file.jnlp" onclick="if (!this.clicked){this.clicked = true; setTimeout('this.clicked = false' ,10000); return true;} return false"> 

The code above only works for disabling the link, but it doesn't get re-enabled after the timeout of 10 seconds.

I've seen that the 'this.clicked' variable isn't true (as it is supposed to be) when I check it in the setTimeout call. Maybe i'm missing some basic JS-knowledge here..

Or perhaps there is a different approach to solve this problem?

like image 496
räph Avatar asked Dec 07 '25 12:12

räph


2 Answers

First add a this function to a Javascript Script block

function Debounce()
{

    var self = this
    if (this.clicked) return false;

    this.clicked = true;
    setTimeout(function() {self.clicked = false;}, 10000);

    return true;
}

Now change your onclick to:-

onclick="return Debounce.call(this)"
like image 122
AnthonyWJones Avatar answered Dec 10 '25 02:12

AnthonyWJones


give this anchor an ID and then change your timeout call to:

setTimeout('document.getElementById("<id>").clicked = false;' , 10000);

I think the 'this' is not evaluated to anything when the timer comes around.

like image 33
Peter Perháč Avatar answered Dec 10 '25 02:12

Peter Perháč