I have the following existing function that I'm unable to access and change. So I've had to cache it and add other functions to it.
The function loads periodically and sometimes load on page load and sometimes doesn't. The additional functions I've added to it , I don't ever want loading on page load however.
I have tried the following with no luck. I can not prevent update_scores from loading on page load , so the if(onSwitch) is true , then and only then I want to add something else to the existing function. Once onSwith is true , then I need all additional functions to always execute when update_scores runs, but initially only after another function click() is called.
function click() {
update_scores(true);
}
update_scores = (function(onSwitch) {
var cached_function = update_scores;
return function() {
let update_scores=true; // always do something below when update_scores after function click ran once
cached_function.apply(this, arguments);
if(onSwitch) {
//Do something only when function click is ran or until onSwith is defined as true
}
};
}());
ok , i was able to achieve by setting a global variable , then changing it when i ran another function
var onSwitch = false;
function click() {
update_scores(true);
onSwitch = true;
}
update_scores = (function(onSwitch) {
var cached_function = update_scores;
return function() {
cached_function.apply(this, arguments);
if(onSwitch) {
//Do something only when function click is ran or until onSwith is defined as true
}
};
}());