By body click start one function after another click start another function

Is it possible to call one function after clicking on the body, and then call another function after the second click?

I tried and then stopped, any ideas?

$("html, body").one("click",function() {
  // to something
}
+3
source share
3 answers

You can try something like this:

var clicks = 0;
$("html, body").on("click",function() {
    if(clicks === 0){
        funcA();
    } 
    if(clicks > 1){
        funcB();
    }
    clicks++;
}

Where funcAandfunc

+4
source

Edit: I changed my answer due to your last addition to your question. See this snippet:

var myFunctions = [];

myFunctions.push(function() {
  $("body").css("background-color", "red");
});

myFunctions.push(function() {
  $("body").css("background-color", "green");
});

$("html, body").click(function(){
  if(myFunctions.length > 0) myFunctions.shift()();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Run codeHide result
0
source

html part:

<input type="button" value="Click Me" />

script part:

$("input").one("click",function() {
    alert("First Function");  
  $("input").one("click",function() {
    alert("Second Function");
  });
});

Demo

0
source

Source: https://habr.com/ru/post/1672172/


All Articles