0

php/html

$i=0

for each ($x as $y => $z){

<a>click me</a>

if (++$i == 5) {break;}
}

javascript

function load_remaining_notifcations(){
    $.post('load_remaining_notifications.php',{},function(data){
        $('#load_remaining_notifcations').html(data);
    });
}

<a>click me</a>被点击五次时,我想运行一个javascript函数。我如何计时 javascript 函数,以便仅在单击我被单击 5 次后才调用它?

4

2 回答 2

1
 var clicks = 0;
 $('a').click(function(){
     clicks++;         
     if (clicks == 5) {
         load_remaining_notifcations();
         clicks = 0; //reset counter for another 5 click or
     }
 });
于 2012-05-19T10:49:45.760 回答
0
// js script
var counter = 0;
function doclick() {
    counter++;
    if (counter>=5) {
        // do something
        alert("OK");
    }
}

// HTML
<a id="myanchor" href="javascript:" onclick="doclick();">click me</a>

编辑:我看到你正在使用 jQuery。

// js
$("a#myanchor").click(function() {
    if (this.counter == undefined) this.counter = 0;
    this.counter++;
    if (this.counter >= 5) {
        load_remaining_notifcations();
    }
});
于 2012-05-19T10:21:39.117 回答