0

我有这个onclick功能,它工作正常:

onclick="transferplayer('palyerlistDIVID','<?=$d_player['id']?>',document.getElementById('play perPosition').value,'teamID','plus','<?=$d_player['teamID']?>','<?=$d_player['price']?>');showmyplayer('myplayerlistDIVID',document.getElementById('playperPosition').value,'plus','<?=$d_player['teamID']?>','<?=$d_player['price']?>')"

我需要的是设置点击之间的时间,所以我添加了这个javascript,每次点击之间有5秒:

var lastClicked = 0;

function onClickCheck() {
    var timeNow = (new Date()).getTime();
    if (timeNow > (lastClicked + 5000)) {
        // Execute the link action 
    } else {
        alert('Please wait at least 5 seconds between clicks!');
    }
    lastClicked = timeNow;
}

问题是:如何将这段代码与onclick函数结合起来?

我试过这个:

onclick="onClickCheck();transferplayer('palyerlistDIVID','<?=$d_player['id']?>',document.getElementById('play perPosition').value,'teamID','plus','<?=$d_player['teamID']?>','<?=$d_player['price']?>');showmyplayer('myplayerlistDIVID',document.getElementById('playperPosition').value,'plus','<?=$d_player['teamID']?>','<?=$d_player['price']?>')"    

但是在点击它时,它总是取值。

4

2 回答 2

2

只需将transferplayer调用移动到您的 if 子句中。

onclick="onClickCheck()"


function onClickCheck() {
    var timeNow = (new Date()).getTime();
    if (timeNow > (lastClicked + 5000)) {
        transferplayer('palyerlistDIVID','<?=$d_player['id']?>',
                        document.getElementById('playerPosition').value,
                        'teamID',
                        'plus',
                        '<?=$d_player['teamID']?>',
                        '<?=$d_player['price']?>');

         showmyplayer('myplayerlistDIVID',
                      document.getElementById('playperPosition').value, 
                      'plus',
                      '<?=$d_player['teamID']?>',
                      '<?=$d_player['price']?>');
    } else {
        alert('Please wait at least 5 seconds between clicks!');
    }
    lastClicked = timeNow;
}

而且,作为记录,不建议使用 PHP 短标签。有关该主题的讨论,请参阅PHP echo vs PHP short tags

于 2012-09-08T07:56:28.250 回答
2

不建议使用内联 javascript。您应该尝试绑定对该元素的单击。

但是你的回答:

onclick="var lastClicked = 0;if(timeNow>(lastClicked + 5000)){ var timeNow = (new Date()).getTime(); if (timeNow > (lastClicked + 5000)) { transferplayer('palyerlistDIVID','<?=$d_player['id']?>',document.getElementById('play perPosition').value,'teamID','plus','<?=$d_player['teamID']?>','<?=$d_player['price']?>');showmyplayer('myplayerlistDIVID',document.getElementById('playperPosition').value,'plus','<?=$d_player['teamID']?>','<?=$d_player['price']?>'); }else{ alert('Please wait at least 5 seconds between clicks!'); } lastClicked = timeNow;"
于 2012-09-08T07:56:41.107 回答