-1

I need to update the time in my setInterval. The value is returned by the function that setInterval is executing.

read_log(); returns an integer.

var loop = setInterval(function(){count = read_log();}, count);

This returns that count is undefiend. So I need to get the count and pass it to setInterval

Gixty
  • 202
  • 4
  • 13
  • Of course it does. `count` is not set when `setInterval` is executed. It is set AFTER `setInterval` is executed. Does `read_log()` pass a different value everytime? – putvande Sep 08 '14 at 23:20
  • You don't need to pass `count` as an argument. The second argument to `setInterval` is the repetition period in milliseconds. – Barmar Sep 08 '14 at 23:20
  • @Barmar I know but I need it to be a variable, not a constant. And this variable needs to be set by the `read_log()` function. – Gixty Sep 08 '14 at 23:22
  • Why are you using the same variable for the repeat interval as you use for the return value from `read_log()`? – Barmar Sep 08 '14 at 23:23
  • What you're doing makes no sense. You need to set the repetition interval before it calls `read_log()`. – Barmar Sep 08 '14 at 23:25
  • @putvande yes, `read_log()` does pass a different value everytime. – Gixty Sep 08 '14 at 23:26
  • @Barmar I know, that is why I am asking for help. I need to pass a value to `setInterval` everytime `read_log()` is executed. And this value is returned by `read_log()` – Gixty Sep 08 '14 at 23:27

1 Answers1

2

If you need to change the repetition interval after each call to read_log(), you can't use setInterval() -- that uses a constent repetition. You need to use setTimeout, so you can change the period each time:

function call_read_log() {
    var count = read_log();
    setTimeout(call_read_log, count);
}
call_read_log();
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Great! This is what I was looking for. I also had to change my `read_log` function so it can return the `count` Here is a good tutorial on how to make a proper `$.ajax` call http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – Gixty Sep 09 '14 at 01:24