Atul Kumar Atul Kumar - 1 month ago 8
jQuery Question

Toggle multiple functions sequentially on button click

I have a set of independent functions -

fn1()
,
fn2()
,
fn3()
,
fn4()
.

I only need one function running at a time but the functions should toggle, on click of a single button, in a sequence
fn1()
[click]
fn2()
[click]
fn3()
[click]
fn4()
[click] back to
fn1()


How do I approach this problem?

Answer Source
function fn1(){}
function fn2(){}
function fn3(){}

var i = 1;

function myclick(){
   if (i == 1)
      fn1();
   elseif (i == 2)
      fn2();
   elseif (i == 3) {
      fn3();
      i = 1;
   }

   i++;
}

Everytime you call myclick, it runs one fn and then adds 1 to i. The next time it runs the 2nd fn, followed by the 3rd.