开发者

Dynamically add/remove function while calling one single function using parameters

开发者 https://www.devze.com 2022-12-14 18:18 出处:网络
I have a function called f0 having some parameters. When i call it, it calls some other functions with some parameters(for example the same parameters).

I have a function called f0 having some parameters. When i call it, it calls some other functions with some parameters(for example the same parameters). The problem is, can i add somehow these functions dynamically?

A simple solution would be something like this:

var callf1=true;
var callf2=true;
var callf3=true;

function f1(params){
//some code
}
function f2(params){
//some other code
}
function f3(params){
//some code
}

function f0(params){
    if(callf1){
      f1();
    }
    if(callf2){
      f1();
    }
    if(callf3){
      f1();
    }

}

function mymain(){
    f0(pa开发者_如何学编程rams);
}

setInterval("mymain()",5000);

This option is much time consuming because every time it has to check the variables. Could i use somehow the trigger option in JQuery using runtime and giving parameters?

I would be glad if you can recommend me an easier and simpler solution.


The current solution is not time consuming since checking some variables once in 5 seconds is far from complicated for the computer. So you probably would want to stick with the current solution - the only thing I would reccomend would be ditching the string-command from setInterval

from:

setInterval("mymain()",5000);

to:

setInterval(mymain,5000);

This way you would bypass an eval statement which is probably much more time consuming than a variable check would ever be.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号