0

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

  • The `setInterval` will itself return a unique ID. Why does the _variable_ need to have a unique ID, exactly? I'm concerned perhaps the question you are asking is related to a less than ideal approach, and if we understood your requirements perhaps a better solution is available. – Alexander Nied Oct 25 '19 at 15:08
  • what is your goal: to have a different implementation for each ['threadTimer'+ID]? or to give to your threadTimer the id which corresponds to the setInterval it comes from? – grodzi Oct 25 '19 at 15:39
  • @AlexanderNied. Thanks for your reply. Could you explain more about how setInterval generates a unique id please? I did not know that. I have experiences of the same function calling itself at faster and faster increments because it was before I started uniquely identifying the interval as an object. – gleddersDotcom Oct 26 '19 at 10:27
  • @user753642 Yes is the simple answer. Imagine objects on a 'page' animating in this instance. However I have had this problem before and I am looking for a general solution. Thanks for your reply. – gleddersDotcom Oct 26 '19 at 10:30

1 Answers1

1

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);
grodzi
  • 5,633
  • 1
  • 15
  • 15
  • That is a really neat solution that had never even occurred to me. Really clever. That solves more issues than just this one too. Many thanks. – gleddersDotcom Oct 26 '19 at 10:40
  • 1
    Without taking away from the answer, I knew my use of 'global' variables would get spotted here. My solution to the global variables issue is something that I bring from VB and php experience. To progress, I sometimes feel the need to break a few rules. :) – gleddersDotcom Oct 26 '19 at 10:44