shinsenter / defer.js

🥇 A lightweight JavaScript library that helps you lazy load (almost) anything. Defer.js is dependency-free, highly efficient, and optimized for Web Vitals.
https://shinsenter.github.io/defer.js/
MIT License
277 stars 45 forks source link

Use delay and waitForUserAction together - execute code on whichever one comes first #133

Closed tno2007 closed 2 months ago

tno2007 commented 2 months ago

We can use the delay integer to defer code: https://codepen.io/tno2007/pen/WNqeEgp

We can also wait for user input, before we execute the code: https://codepen.io/tno2007/pen/OJeLjYR

But it does not seem like the library can use a combination of both together: (The custom code does not execute after the delay set. The page waits for user input) https://codepen.io/tno2007/pen/bGPbrPW

I'm not sure if this was the intention. But do you think you can add such functionality: (It executes your code whenever the delay is over, or whenever the user starts interacting with the site) https://codepen.io/tno2007/pen/PorYKMG

shinsenter commented 2 months ago

Hi @tno2007

Since v3.8.0, you can set a timeout period in milliseconds for the Defer.lazy variable to the waitForUserAction argument. If no user interaction occurs within this timeout period, the scripts will still execute.

Your 3rd code may be like this.

Defer(update, 0, 1000);

View more in #131.