我有这个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']?>')"
但点击它总是需要值。
答案 0 :(得分: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。
答案 1 :(得分: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;"