gleddersDotcom
gleddersDotcom

Reputation: 3

Create a unique function name on the fly for shared timer

I'll start with the exact nature of the problem and then give some background. I am trying to name a function -threadTimer- and give it a random unique identifier, such as 'threadTimer'+ID. A randomly generated ID would work fine. Then, I need to use setInterval on it, to make it fire repeatedly and therein lies my coding problem. I have tried every variation of new, function, function as an object and I just can't get my head around it. You'll notice that the function I have created is an object and perhaps this is where I'm going in circles.

OK, the background I mentioned. threadTimer is fired by a master timer co-ordinating several threads. That's why you'll see I have generated a 'global' object for reference elsewhere. similar HTML entities can fire threadTimer at the same time, hence my requirement to make each instance unique.

window['GlblThreadExe'+ID]=setInterval(function(){threadTimer(elid,parent,lft,top,diameter,point,bStyle,color,grp,startTime,size,ID,counter,div,divwth,divht,wthIncrement,htIncrement,lftStart,topStart,lftIncrement,topIncrement)},interval);

function threadTimer(elid,parent,lft,top,diameter,point,bStyle,color,grp,startTime,size,ID,counter,div,divwth,divht,wthIncrement,htIncrement,lftStart,topStart,lftIncrement,topIncrement){
    // more code
}

In truth, I think its the volume of parameters that I'm passing that's confusing my syntax. Any help appreciated

Upvotes: 0

Views: 50

Answers (1)

grodzi
grodzi

Reputation: 5703

Avoid polluting window

Generally instead of polluting the global namespace you can store your setInterval ids in some variable

let intervalIds = {}
intervalIds['GlblThreadExe'+ID] = setInterval(function()...)

If really necessary, then store intervalIds to window

window.intervalIds = intervalIds;

Wrap your anonymous function

When you create the "clock", do not call setInterval directly: Here, createTimerWithId will return a function which calls threadTimer

  1. Dirty id generation

Use a timestamp, and mix it with some random stuff. Or better use a UUID

setInterval(createTimerWithId(), 1000)
function createTimerWithId(){

    let id = Date.now()+Math.random(); //no lib, oneliner. good enough to debug

    return function(){//the same function you gave to setInterval in your example
        threadTimer(id, ...)
    }
}
  1. We can do better

In 1. we generated an id on the fly and thus

  • your code is not testable (id will always change(well except if you mock Math and Date...)).
  • your id is ugly (a float...)
  • it will be hard to know from which setInterval you come from

instead, give it the ID.

function createTimerWithId(ID){

    return function(){//the same function you gave to setInterval in your example
        threadTimer(ID, ...)
    }
}

window['..'+ID] = setInterval(createTimerWithId(ID));

shorter version being

window['..'+ID] = setInterval((id=>{
    return function(){
        threadTimer(id, ...)
    }
})(ID),1000);

Upvotes: 1

Related Questions