admin管理员组

文章数量:1292506

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?

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?

Share Improve this question asked Apr 30, 2009 at 13:31 räphräph 3,6849 gold badges36 silver badges42 bronze badges 2
  • Try to use functions instead of doing everything inline.. it will be easier to debug the code. Also, consider learning JQuery. It would make it a lot easier to add a visual effect to the "disabling" of the link. – Thomas Stock Commented Apr 30, 2009 at 13:40
  • I don't like inline code either, but I'm working in some JSF-view, and I must admit, I don't know how and where to declare a JS-Function there. But that's another story, I suppose – räph Commented Apr 30, 2009 at 14:19
Add a ment  | 

5 Answers 5

Reset to default 3

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)"

using your code modifying only a small part it may work

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

looks like people say that didnt work and it didnt form a closure around 'this'

The this object is not defined in the code being evaluated by setTimeout which is done in the global scope.

Give the link an ID then use getElementById e.g.

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

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 es around.

var a=this;
setTimeout(function(){a.clicked = false} ,10000);

本文标签: javascriptHow to disable Link for some time after being clickedStack Overflow