0

I need to call a function every 100ms, which is easy enough, but what if I need that function to accept a parameter?

The problem is that I create an object, and periodically need to update it. I have tried setting the object reference to a global, that didn't work. I tried setting it to a function variable, still no luck. Apparently I need to pass in the object, which I cant't figure out how to do using setInterval. There has to be a trick to this?

The code below works on forst call, but after that it fails at:

    setCounterText.segment.DisplayText("AAABBB");

And complains that setCounterText.segment.DisplayText() is not a function...

Thanks...

window.onload = function ()
{

        setInterval(setCounterText, 1000);
}




function setCounterText()
{

  //"use strict";

  var num;

  if(!setCounterText.isInit)
  {
     num = 0;
     setCounterText.isInit=true;

     var canvas = document.getElementById('c');
     var container = document.getElementById('container');
     canvas.width = container.clientWidth;
     canvas.height = container.clientHeight;

     // Create a new sixteen segment display
     setCounterText.segment = new SixteenSegment(1, canvas);

     update(setCounterText.segment);

     setCounterText.segment.DispayText("T-000:00:00.0");
   }

   num++;

   setCounterText.segment.DisplayText("AAABBB");

}
4
  • I think you're timing is off. Currently it's invoking every 1 sec. 1000 != 100ms... Commented Oct 6, 2015 at 18:20
  • Yeah, its easier to watch the debugger go every second than every .1 seconds. I'll fix that if I can get it to work. Commented Oct 6, 2015 at 18:24
  • Your question is unclear, do you want to send a parameter to your setCounterText function using setInterval? Commented Oct 6, 2015 at 18:27
  • Yes, I either want to do that or I want to find a way to save the values of isInit and segment across calls to the setCounterText() function. Commented Oct 6, 2015 at 18:32

1 Answer 1

2

You can create another function to act as a clojure for the setCounterText function and pass that as a parameter to setInterval.

 setInterval(function() {
   setCounterText(anotherParameter);
 }, 1000);

That will capture your parameter and call the setCounterText function whenever the interval triggers.

Regarding the error you are getting, it's impossible to say without knowing the code in the SixteenSegment function but it should have a property set on it called DisplayText.

Sign up to request clarification or add additional context in comments.

3 Comments

Thats a cool idea, thanks, i'll try that. As for calling DisplayText(), it's the same method I'm calling inside the isInit condition. Apparently the value of segment is not being persisted across calls to this function.
Maybe somethig in the update function is causing it to be ok?
I was just checking that, and I can successfully call DisplayText() from inside the update function, and I can't see any reason why. I need to get this going, so I'll just call update() from setCounterText() and that seems to work. Now I just need to write the code that sets the correct value of the timer and I'll be good. Not what I planned but if it works I can move on. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.