我有这个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']?>')"


但是单击时,它始终会取值。

最佳答案

不建议使用内联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;"

关于php - onclick函数中的if/else语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12329043/

10-12 17:30