0

My original question was about a block of code similar to this:

delayTime = 1000;
reducedDelay = 0;

setInterval(
function(){

//Code which calculates reduced delay

    reducedDelay = /* -- Any integer -- */;

    delayTime = 1000 - reducedDelay;

},delayTime)

delayTime is a global scope variable, so to my understanding, delayTime would be updated by the expression delayTime = 1000 - reducedDelay; and so the setInterval would call the function again with the updated delayTime delay... right?

And this is just one use, but I was not able to find a way to actually check the delay of a setInterval() or setTimeout(). How could one check the delayTime (the second argument)?

A better way to phrase it: How could one check the setTimeout() or setInterval('param 1', 'param 2') <-- 'param 2' being the delay, how would one check if the setInterval is actually performing correctly? (I.e. delaying the next call of 'param 1' by the correct time.)

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
DR. Palson_PH.d
  • 301
  • 1
  • 3
  • 11

1 Answers1

0

When you call setInterval you send the value, not the variable reference. So after you call it, it doesn't matter if you change it.
But answering your question about how to check, you can't get the value it is using but you can measure it.

reduce = 5000;
lastCall = (new Date()).getTime();
setInterval(function(){
    reduce-=1000;
    console.log("New reduce value : "+reduce);
    newTime = (new Date()).getTime();
    console.log("Time since last call : "+(newTime - lastCall));
    lastCall = newTime;
},reduce);

You can copy paste this code in the console of this page to test it. ;)
btw, if you wanted your code to work you could make a recursive call to setTimeout. That would do the trick.

Mark E
  • 3,403
  • 2
  • 22
  • 36
  • Thank you for the answer, I ran the code and the time between calls hovered around 5000ms, I thought the way I did my code it would reduce the delay, and make it as acurrate as possible. But what you and @StephenBugsKamenar seem to be saying is that the variable's value after the initial call does not matter. But this leaves me confused, how can one fight latency? Like you said, recursive setTimeout calls are way to go huh? – DR. Palson_PH.d Feb 12 '17 at 23:27
  • Update, i used a recursive setTimeout call with this format: recursiveLoop(); function recursiveLoop(){ setTimeout(function(){ //code which calculates the latency and reduces delayTime by the latency, similar to original example recursiveLoop(); } ,delayTime) } Now in this example, Since I am calculating a new delayTime which compensates for latency, would the setTimeout be called with the compensated for delayTime, or will it be fixed, like my setInterval example – DR. Palson_PH.d Feb 13 '17 at 00:25
  • I'm not sure which is your final purpose, but I think that people who want to keep accurate usually use `requestAnimationFrame()` which gives you the time when your function was called and you calculate changes based on the last call time – Mark E Feb 13 '17 at 00:47
  • My purpose it to create a latency free timer. You mentioned earlier that setInterval works at a fixed value, so trying to reduce delayTime by latency is useless, you also mentioned something about a recursive setTimeout(). I was just wondering HOW I would implement that recursive setTimeout() so that I have a variable delayTime (based on latency). I am novice programmer, and I wasn't able to find much on recursive setTimeout() on the web. But you seem to know more than me. – DR. Palson_PH.d Feb 13 '17 at 21:04
  • The code you used on the example in your comment may work. Although you could just make a `setInterval` faster than what your timer would go and use the real time from `Date()` to calculate when something needs to happen. – Mark E Feb 13 '17 at 21:12
  • Just looked for js accurate timer in google and the [first answer](http://stackoverflow.com/questions/29971898/how-to-create-an-accurate-timer-in-javascript) looks exactly like what you want. – Mark E Feb 13 '17 at 21:13
  • That's actually a really smart idea, and thanks for the link. Altough I searched my *** off for recursive setTimeouts. – DR. Palson_PH.d Feb 13 '17 at 22:13