0

I have this onclick function and it is working fine:

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']?>')"

What I need is to set a time between clicks, so I added this javascript with 5 seconds between each click:

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;
}

The question is: how can I combine this code with onclick functions?

I tried this:

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']?>')"    

But on clicking it takes the values always.

2 Answers 2

2

It is not advisable to use inline javascript. You should try to bind the click on that element.

But your answer:

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;"
Sign up to request clarification or add additional context in comments.

1 Comment

Why not move this all into a function?
2

Just move the transferplayer call into your if clause.

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;
}

And, for the record, use of PHP short tags is not recommended. See PHP echo vs PHP short tags for a discussion of the subject.

1 Comment

About the short tags... I agree when <? is used, but <?= is completly ok, even php 5.4. advices the use > docs.php.net/manual/en/migration54.new-features.php (<?= is now always available, regardless of the short_open_tag php.ini option.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.