0

I am trying to use setInterval with setTimeout in JavaScript.

var temArr = []

let timer = setInterval(() => {
  var target = Math.random()
  temArr.push(target)
  console.log(6, target, temArr[temArr.length - 1], target === temArr[temArr.length - 1]) // one

  setTimeout(() => {
    console.log(9, target, temArr[temArr.length - 1], target === temArr[temArr.length - 1]) // two
  }, 500);
}, 100)

setTimeout(() => {
  clearInterval(timer)
}, 10000);

I expected that the last value of one and two would be true.

I expected that every time when the logger print, both one and two have the same value of the third parameter, that means target equals temArr[temArr.length - 1]

But the result is that one is true, and two is false

But the intermediate result of two is false, and the last value is true, while one is alway true

When I switch the timeout value, and the result is true that means set the interval with value 500 and the timeout with value 100, at this time both target === temArr[temArr.length - 1] is alway true every time the logger print

could you please tell me why?

2
  • The last line in the console for me is 9 0.357113447138378 0.357113447138378 true so two is true as you expected. Commented Oct 29, 2019 at 11:37
  • I am so sorry, I didn't express the problem well, and I am already update the question Commented Oct 30, 2019 at 1:00

1 Answer 1

2

They both returned true for me, just different order:

Ok setInterval VS setTimeout:

setInterval - will run the function inside EVERY x ms. which means:

setInterval(() => {
  console.log('I will run every 100ms')
}, 100)

Will run EVERY 100ms.

setTimeout - will run the function inside AFTER x ms. which means:

setTimeout(() => {
   console.log('I will run after 10 seconds')
}, 10000);

will run AFTER 10 seconds.


So doing this:

let timer = setInterval(() => {
  console.log('I will run every 500ms';   

  setTimeout(() => {
    cosole.log('I will run AFTER 500ms EVERY 500ms');
  }, 500);
}, 100)

The log inside setTimeout will run 500ms AFTER the first log has been triggered and will trigger every 100ms.


EDIT - Answer to user's edited question:

For clearer logs, I modified your code:

var temArr = []
var intervalOrder = 0;
var timeoutOrder = 0;

var timer = setInterval(() => {
  var target = Math.random()
  temArr.push(target)
   intervalOrder++

  console.log('ONE - SET TIMEOUT: ', {order: intervalOrder, target, tem: temArr[temArr.length - 1], result: target === temArr[temArr.length - 1]})

  setTimeout(() => {
timeoutOrder++
    console.log('TWO - SET TIMEOUT: ', {order: timeoutOrder, target, tem: temArr[temArr.length - 1], result: target === temArr[temArr.length - 1]}) // two
  }, 500);
}, 100)

setTimeout(() => {
  clearInterval(timer)
}, 1000);

Notice how TWO - SET TIMEOUT doesn't fetch the same result as ONE - SET TIMEOUT? It's because it doesn't fetch the value of that variable when it was called but it fetches ON TRIGGER which means that the value already changed because setInterval has shorter time.

What you can do for this is to call the setTiemout on a different function so it will reference its value on the parameter instead of the newly generated one.

var temArr = []
var intervalOrder = 0;
var timeoutOrder = 0;

var logSecond = (target) => {

  setTimeout(() => {
timeoutOrder++
    console.log('TWO - SET TIMEOUT: ', {order: timeoutOrder, target, tem: temArr[temArr.length - 1], result: target === temArr[temArr.length - 1]}) // two
  }, 500);
}

var timer = setInterval(() => {
  var target = Math.random()
  temArr.push(target)
   intervalOrder++

  console.log('ONE - SET TIMEOUT: ', {order: intervalOrder, target, tem: temArr[temArr.length - 1], result: target === temArr[temArr.length - 1]})
logSecond(target)
}, 100)

setTimeout(() => {
  clearInterval(timer)
}, 1000);

^^ on the snippet above, the target and result are now the same for both logs.

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

2 Comments

Yeah, thank you :) . Now I know the difference between setInterval and setTimeout (I am so sorry, I didn't express the problem well, could you please help me again ? )
@Huanfeng.Xu I edited my answer for your edited question - kindly check the answer above (and accept if this is correct)

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.