requestIdleCallback | A requestIdleCallback shim/polyfill | Reactive Programming library
kandi X-RAY | requestIdleCallback Summary
kandi X-RAY | requestIdleCallback Summary
This is a polyfill/shim for the requestIdleCallback and cancelIdleCallback API. Also fixes early API implementation. For more information see the Cooperative Scheduling of Background Tasks Draft.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Runs task .
- Debounce a function .
- Schedule a task that will be executed at once .
- Called when the input is active .
- Aborts a running animation
- Adds a task to the idle task queue .
- Calls the callback to cancel a task with the given id at the given time .
- Schedule and schedule a new task .
- Schedule and restart the loop
requestIdleCallback Key Features
requestIdleCallback Examples and Code Snippets
Community Discussions
Trending Discussions on requestIdleCallback
QUESTION
I've just found out about window.requestIdleCallback
and I am wondering on the difference with just a plain promise.
As far as I know JS is already good at queuing promises, so I do not see any benefit unless requestIdleCallback
uses a different queue with lower priority. In that case I would assume it is great because I would like having a way to make clear which code has lower priority than rendering.
ANSWER
Answered 2022-Feb-07 at 02:38window.requestIdleCallback
simply runs the function during the browser idle periods to avoid impacting animations and input responses etc.
Promises are just a way of using asynchronous code. That code may be run while the browser is not idle (if the promise is not pending), and could possibly impact latency critical events.
Therefore, promises may or may not run during the browser idle period, but the requestIdleCallback
will always run in the idle period (assuming the timeout isn't exceeded).
Please note, these 2 concepts are not interchangeable. Yes, you can run asynchronous "promise" code within the requestIdleCallback
, but the callback should not be used to replace the functionality of a promise. That is, you shouldn't replace the use of promises with the callback, but use them together if appropriate.
Here's some info about window.requestIdleCallback
:
https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback
And Promises here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
QUESTION
We have a large grid rendering lots of expensive components. To reduce the load on the page we use a vitualized list and only render the visible portion at any given moment. This means that whenever someone scrolls, there is flash of white as the components previously outside of the visible area render for the first time.
To get around this we can define an "overscan" area - an amount of items that gets rendered around the visible area that is immediately visible on scroll. Unfortunately that means that these components are now updating alongside page state, which is in our case an expensive operation for any reasonable amount of overscan.
I was thinking one way to resolve this would be to defer the update/render lifecycle of components outside of the visible area to when the call stack clears, utilizing a setTimeout
, or perhaps using a requestIdleCallback
. I have not been able to find a way of doing this in React 16/17 - any ideas?
ANSWER
Answered 2021-Oct-22 at 11:24You can give the components a property telling them whether they're in overscan or not, and have shouldComponentUpdate
return false
if inOverscan
is true
for both the previous and current props (or if using function components, React.memo
's comparison function would return true
if inOverscan
is true
for both the previous and current props).
Here's an example:
QUESTION
How do I make A and B run in parallel?
...ANSWER
Answered 2021-Jul-04 at 04:35The two example functions simply execute synchronously one after the other on the same "thread" (JS effectively has only one thread available to such scripts).
The use of async
is irrelevant here because no truly asynchronous operation is occurring in function A - it is simply a busy while loop - so it completes in full before execution can move to anything else.
If function A had called an actual asynchronous operation (such as a HTTP request - not simply a synchronous operation wrapped in an async
function), then function B may have a chance to start up (in which case B would complete entirely before the execution returned to A, because B is also only contains a synchronous, busy while loop).
Parallel processing can be achieved with WebWorkers which allowing running on background threads (actual separate threads).
QUESTION
In a browser, I am trying to make a well-behaved background job like this:
...ANSWER
Answered 2020-Jul-25 at 17:23I think this is really dependent on what exactly you are trying to achieve though.
For example, you could initialize your web-worker on loading the page and make it run the background-job, if need be, then communicate the progress or status of the job to the main thread of your browser. If you don't like the use of post-message
for communication between the threads, consider user Comlink
However, if the background job you intend to do isn't something worth a web-worker. You could use the requestIdleCallback
API. I think it fits perfectly with what you mentioned here since you can already make it recursive. You would not need a timer anymore and the browser can help you schedule the task in such a way that it doesn't affect the rendering of your page (by keeping everything with 60fps).
Something like =>
QUESTION
I have the following function, that zooms out on images once they enter the viewport. All images are lazyloaded, which is why I want to start the zooming behavior only when the image is fully visible. The event to listen to is called 'lazyunveilread'. Then there is a css transition to fade in the image. Once both things are done, the zooming should be initialized.
Unfortunately, I don't know how to do that. I tried installing the event listener on the event but that only works for the first element and not for the others.
...ANSWER
Answered 2020-May-29 at 12:32So I changed your markup a little bit:
QUESTION
Are functions passed to requestIdleCallback
guaranteed to run when no timeout
is specified? (Assuming we aren't in some contrived scenario, specifically engineered to avoid idle state indefinitiely)
And if a timeout
is specified, is there a guarantee around order of execution? e.g.
ANSWER
Answered 2020-May-04 at 06:55Cooperative Scheduling of Background Tasks says:
During an idle period the user agent will run idle callbacks in FIFO order until either the idle period ends or there are no more idle callbacks eligible to be run. As such, the user agent will not necessarily run all currently posted idle callbacks within a single idle period. Any remaining idle tasks are eligible to run during the next idle period.
Therefore, I believe with your example, fnOne
, fnTwo
and fnThree
would be executed in that order.
QUESTION
This is a bizarre one. We have a Laravel website, and on said site we have a timer per user, where they get 15 minutes of being inactive before being booted.
We do this through a timer that sits on the page in a react component, it works as we want it to, but now we have a new issue: If a user is logged in and shut the lid of their laptop the website should boot them. Banks do this, Schools and Universities do this, Government sites also do this. So it is possible, just not sure how.
We do use web sockets, using laravel-websockets library and Echo. What I would like to see happen is:
- Once you close your laptop boot you to the login screen. So the next time you open the laptop and login, and see the browser you are on the login screen. It doesn't have to happen that quickly, but we need a way to send something to the front end basically telling them to refresh the page, once the session is killed, we set the session lifetime on laravel of 15 minutes.
Some people have suggested in other similar questions:
- to create a custom web-socket handler
- To compare the session cookie (in the browser) with the user cookie on the back end.
- To have a timer running on the front end (we do, it just stops when you close the laptop lid)
The most popular one seems to be using web-sockets, listening for the user to disconnect and then boot them, which is fine and all, but then how do you send a request to a browser thats suspended to then boot them?
I have found requestIdleCallback() But again, I don't think this is what I want if I already have a heartbeat timer on the site. It also doesn't work in all browsers.
I am very lost here on how to accomplish this, the example I can give is:
Log in to your bank, put your computer to sleep, wait 15-20 minutes, awaken the computer, log in and see your bank now has you on the login screen. That's what I want. But I don't know how to accomplish that.
You cant send events to a "sleeping" browser from the back end, and while yes this would have to be a back end solution, how do you update the front end then, so that they are on the logout screen when they reawaken the laptop or computer?
...ANSWER
Answered 2020-Mar-23 at 20:31So Idea is behind setInterval and Sockets, setInterval is supported in most browsers and javascript WbsocketApi is supported in almost everybrowser.
Brief overview: setInterval() - this function behaviour is following when your computer is at sleep/suspended/hibernate mode it is paused and when you are at awaken mode it resumes itself.
The following code does the following, at first(maybe at the same time but) it starts php server_socket listening to the connections,
than javascript websocket api sends current timestamp in Unix timestamp milliseconds in every 2 seconds you can have 1 second it is up to you.
after that php server socket is getting this time and checks if it has anything like previous time to compare, when the code is first instantiated php does not has anything like previous time to compare it to the time which was sent from javascript websocket, so php does nothing but saves this time in the session called 'prev_time' and waits for another time data to be recieved from javascript socket, so here begins second cycle.
when php server socket new time data from javascript WebsocketApi it checks it has anything like previous time to compare to this newly received time data, it means that php checks if session called 'prev_time' exists, as we are in the second cycle php discovers that it exists, grabs it's value and does following $diff = $new_time - $prev_time
, $diff will be 2 seconds or 2000 miliseconds because remember our setInterval cycle happens in every 2 seconds and time format we are sending is in miliseconds,
than php checks if($diff<3000)
if difference is less than 3000 if it is it knows that user is active, again you can manipulate this seconds as you wish, I choose 3000 because possible latency in the network which is almost impossible but you know I am always cautious when it comes to networks, so let's continue, when php determines that user is active php just resets 'prev_time' session with the value of $new_time
which was newly received and just for testing purposes it sends message back to javascript socket,
but if $diff
is more than 3000 it means that something paused our setInterval and there is only way it can happen and I think you already know what I am saying, so in the else
logic of ( if($diff<3000)
) you can logout user by destroying specific session and if you want to redirect you can send some text to javacript socket and create a logic which will execute window.location = "/login"
depending on the text, that's it here is the code:
First it is index.html file just to load javascript:
QUESTION
In my React-App (create-react-app) I use a selector (created with reselect) to compute derived data from stored data.
The Problem is, the selector takes a long time to compute. I would like to show a spinner (or a message) on the user interface. But each time the selector is recomputed the ui freezes.
I read a lot of stuff (Web Worker, requestIdleCallback, requestAnimationFrame) and try to make my own React hook but I am stuck. I cannot use the selector in callbacks.
The searched result is simply to get the ui refreshed before the selector is recomputed.
...ANSWER
Answered 2020-Mar-19 at 09:41That's my solution. I don't know if it's "good" or if it breaks some rules of react or reselect, but it works. Maybe you can assess that?
The code is simplified to improve readability.
The idea is, the selector returns a Promise and I call requestAnimationFrame
before computing the data to get a chance to refresh the ui.
selector.js:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install requestIdleCallback
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page