question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wait for reflow or render to finish after applying style

See original GitHub issue

How should I wait for the re-render to occur after I apply a style to a div? Is there any event for this?

Or else I am getting an error of Error: Could not compute box model. when calling DOM.getBoxModel() right after applying a style.

const CDP = require('chrome-remote-interface');
const chromeLauncher = require('chrome-launcher');

chromeLauncher.launch({
  port: 9222,
  chromeFlags: [
    '--disable-gpu',
    '--headless',
  ],
}).then(async (chrome) => {
  const client = await CDP({ port: chrome.port });
  client.on('error', (err) => {
    console.error(err);
  });

  const { Page, DOM, CSS } = client;

  await Page.enable();
  await Page.navigate({
    url: 'http://github.com',
  });
  await Page.loadEventFired();

  await DOM.enable();
  await CSS.enable();

  const { root: { nodeId: docNodeId } } = await DOM.getDocument();
  const { nodeId: formId } = await DOM.querySelector({
    nodeId: docNodeId,
    selector: 'form[action="/join"]',
  });

  await CSS.setEffectivePropertyValueForNode({
    nodeId: formId,
    propertyName: 'display',
    value: 'none',
  });

  await CSS.setEffectivePropertyValueForNode({
    nodeId: formId,
    propertyName: 'display',
    value: 'block',
  });

  // Here the above code applies the style
  // but doesn't wait for the render to finish.

  await DOM.getBoxModel({ nodeId: formId });

  // So after this line it is throwing an error saying
  // Could not compute box model

  client.close();
  chrome.kill();
}).catch((err) => {
  console.error(err);
});
Component Version
Operating system OSX 10.12.6
Node.js v8.2.1
Chrome Canary v62.0.3187.0
chrome-remote-interface v0.24.3

Is Chrome running in a container? NO

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
paulirishcommented, Aug 16, 2017

I’d wait for two sequential requestAnimationFrame()s to fire. then you can be sure these changes have applied.

1reaction
paulirishcommented, Aug 17, 2017

Is this the right approach of doing this?

It’s not a huge deal IMO but i would just hardcode the 2 nested raf inside a single runtime.evaluate…

roughly like

  await Runtime.evaluate({
    expression: `new Promise((resolve) => 
                  window.requestAnimationFrame(_ => 
                  window.requestAnimationFrame(resolve)));`,
    awaitPromise: true
  });
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to properly wait for browser reflow/repaint to finish
Let's say I have a complex HTML component that I want to animate, but every time it needs to be animated, several things...
Read more >
Understanding Repaint and Reflow in JavaScript | The Startup
parts of the render tree (or the whole tree) will need to be revalidated and the node dimensions recalculated. This is called a...
Read more >
Avoid Reflow & Repaint - Hi Masterclass #7 - YouTube
Are you ready to find out more about how to "Avoid Reflow & Repaint"? Your well-known Masterclass expert Pedro Oliveira will guide you ......
Read more >
How to wait for the browser to finishing reflow in javascript?
HTML : How to wait for the browser to finishing reflow in javascript? [ Beautify Your Computer : https://www.hows.tech/p/recommended.html ] ...
Read more >
Preventing Content Reflow From Lazy-Loaded Images
Reflow is a problem because it's a user-blocking operation. It slows down the browser by forcing it to recalculate the layout of any...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found