1

i want to handle the trigger.click event

this action works once , but how can i handle and edit it

for example click 10 times or click after delay and ....

how should it be customize ?

my code :

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
  function dio() {
    $("#bin").append("hi" + "<br>")
  }
</script>
<button id="btn" onclick="dio()">|||</button>
<div id="bin">

</div>

<script>
  $("#btn").trigger('click');
</script>

this action print a hi after loading the page , how can i set click count , and time and stop ?

8
  • Can you describe what do you mean when asking for "i set click count , and time and stop"? It is absolutely not clear. Please, update your question Commented Feb 5, 2021 at 18:52
  • trigger.click event , only click once , and my main question is how can i make trigger.click who clicks more time ,(10 times) and how can set time(delay) between clicking and stop action ( for the last click) Commented Feb 5, 2021 at 18:54
  • If you just want do trigger it 10 times then pack it inside a loop, or use setIntervall() and trigger it for example every second Commented Feb 5, 2021 at 18:57
  • this is true , but 10 clicks are activated in same time Commented Feb 5, 2021 at 18:59
  • i want to have delay by each clicking , Commented Feb 5, 2021 at 19:00

1 Answer 1

2

You can use the setInterval() to trigger it every second. In order to stop the click after ntimes you can add a data field to your button.

The snippet:

function doit() {
    $("#bin").append("hi" + "<br>")
}


setInterval(() => {
    var max = +$("#btn").data('max') - 1;
    if (max >= 0) {
        $("#btn").data('max', max);
        $("#btn").trigger('click');
    } else {
        clearInterval(timer);
    }
}, 1000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button id="btn" data-max="10" onclick="doit()">|||</button>
<div id="bin">

</div>

Sign up to request clarification or add additional context in comments.

5 Comments

this is true, , thx but can you make it for me via jquery completly
@Alex I am not sure what do you mean by jquery completly. It is already because you don't use DOM manipulation setInterval doesn't change anything to this fact.
this code works , but this code have lot of warning in phpstorm
@Alex you can get rid of the var timer because you never use it.
@Alex the return value of setInterval is not important, you can get rid of it. It is only important when you later on want stop it

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.