1

I'm trying to get the flag value in the console.log(). But when I equalize flag to entry.isIntersecting it returns undefined. In the commented console.log function I get the correct result of flag. I tried async method too but it gave same result.

  let flag = false;
    const observer = new IntersectionObserver(function(entries, observer){
        entries.forEach(entry => {
            flag = entry.isIntersecting
            /*console.log(flag)*/
            return flag
        })
    }, options)
    
    console.log(flag)
eva
  • 29
  • 6
  • Your can only work with your `flag` variable inside the `IntersectionObserver` handler. This is because it relies on the `entries` parameter of that function. The final `console.log(flag)` in your code will always display `undefined` because it's outside the intersection observer. You need to change your logic to work with the IO callback properly. If you could update your question with details of exactly what you're trying to do we can provide a more beneficial answer. – Rory McCrossan Jan 07 '22 at 15:13
  • 1
    Here's [another question](https://stackoverflow.com/q/23667086/519413) covering a very similar problem caused by asynchronous logic instead of callback functions, but the pattern you need to follow would be the same. – Rory McCrossan Jan 07 '22 at 15:14
  • Oki doki, thanks a lot! @RoryMcCrossan – eva Jan 07 '22 at 15:15

0 Answers0