Jump to content
Sign in to follow this  
alessiopaoletti

How I can add intersection observer's callback with window.requestIdleCallback()?

Recommended Posts

I have an intersection observer for one animation, but i want to use window.requestIdleCallback()

Where I should add the code window.requestIdleCallback(callback_1)?

my script:

if (document.querySelector('#weddings')) { 

let observer_1;
let Element_1;
let prevRatio = 0.0;
let options = {
    root: null,
    rootMargin: "0px",
    threshold: buildThresholdList()
};

// Set things up
        Element_1 = document.querySelector("#img-1");
        initObserver();

//////////////////////////////////////////////////////////////////////////////

function initObserver() {
    observer_1 = new IntersectionObserver(callback_1, options);
    observer_1.observe(Element_1);
}

/////////////////////////////////////////////////////////////////////////////

function buildThresholdList() {...}

/////////////////////////////////////////////////////////////////////////////

var gallerySroll_1 = anime({...});

////////////////////////////////////////////////////////////////////////////

function callback_1 (entries, observer_1) {
  entries.forEach((entry) => {
    if(entry.boundingClientRect.top <= 0 && entry.intersectionRatio != prevRatio){
                gallerySroll_1.seek(gallerySroll_1.duration * (entry.intersectionRatio));
    }else{ gallerySroll_1.seek(1000); }
    prevRatio = entry.intersectionRatio;
  });
}
}

if I add the code at the start after the if before anything i got the error:

TypeError: entries.forEach is not a function
    callback_1 https://alessiopaolettidesign.it/ph/js/main.js:343

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...