1

So what I want to do is console.log a specific element of my array of three nodes.

const sentinel = document.querySelectorAll('.sentinel');

    sentinel.forEach(function(element, index){
    setInterval(function(element) {

        if(index === 0){
            console.log(element);
        }

    }, 3000);
});

I've googled, and I've found this post: Get a specific element within forEach loop, which does ask the same question. However, when I console log the element I expect to get the node on the 0 index. However, I get a null element.

2
  • 6
    Remove the element parameter from the setInterval callback Commented Jan 4, 2020 at 12:11
  • Have a look at the documentation for setInterval. There it is mentioned how you can pass something to the callback invoked by setInterval() (which in this case is not necessary because it's in a .forEach()) Commented Jan 4, 2020 at 12:14

2 Answers 2

2

You can remove element from setInterval because inner function setInterval has an access to the variables of outer function such as element. Such behavior is called closure.

In addition, it is possible to use arrow functions to make shorter and cleaner your code. Moreover, arrow functions:

An arrow function does not have its own this. The this value of the enclosing lexical scope is used; arrow functions follow the normal variable lookup rules.

sentinel.forEach((element, index) => {
   setInterval(() =>{

    if (index === 0){
        console.log(element);
    }

}, 3000);
Sign up to request clarification or add additional context in comments.

5 Comments

The callback can get arguments. It needs to be passed after the delay parameter to setInterval
What do arrow functions have to do with the problem/answer?
"You can remove element from setInterval" - Why?
Better but I still miss an explanation why just using element works. But why the quote on this for arrow functions? Neither you nor OP use this anywhere. And if you're already adding unrelated stuff then also add a link to the question why you can not always simply exchange a function() { ... } with an arrow function.
@Andreas 1.*I still miss an explanation why just using element works* Because inner function setInterval has an access to the variables of outer function such as element. Such behavior is called closure 2. But why the quote on this for arrow functions? This quote is additional info to describe important feature of arrow functions
1

So actually you need to bind element into setInterval function

const sentinel = document.querySelectorAll('.sentinel');

sentinel.forEach(function(element, index){
    (function (_element, _index){
        setInterval(function() {

           if(_index === 0){
             console.log(_element);
           }

        }, 3000);
    })(element, index);
});

1 Comment

There is no need for IIFE here. You can pass arguments to the callback from setInterval itself. Anything passed after 3000 (delay parameter), will be used in the callback. So, setInterval(function (e, i) {... }, 3000, element, index) should work. Documentation

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.