You can generally solve this problem by wrapping an array of functions that you would like to alternate (two in your case, but it can be any number) in a function that will call the module on the number of attempts to call a new function.
This will work with parameterized functions if all the functions you pass accept the same parameters.
function alternate(arrayOfFunctions) {
var counter = 0;
return function() {
var f = arrayOfFunctions[counter++ % arrayOfFunctions.length];
return f.apply(this, arguments);
}
}
function doA() { console.log(doA.name);}
function doB() { console.log(doB.name);}
function doC() { console.log(doC.name);}
var newFunction = alternate([doA,doB,doC]);
newFunction();
newFunction();
newFunction();
newFunction();
source
share