Delay JavaScript holds back the scripts on your pages until the visitor interacts with the page, for example by moving the mouse, scrolling with the wheel, pressing a key or touching the screen. The first view loads with much less JavaScript, which often improves speed scores a lot. The trade-off is that anything built by those scripts waits for that first interaction.
Available in: Powered Cache (free)
Where to find it: Powered Cache → File Optimization → JavaScript
How delaying works #
- Before the page is sent, Powered Cache changes the type of every regular script (external files and inline code) so the browser does not run it. Scripts with another type, such as structured data (
application/ld+json) or JavaScript modules, are left alone. - A small loader script is added to the page head.
- On the visitor’s first key press, mouse movement, mouse click, touch or mouse wheel scroll, the loader runs the delayed scripts in their original order.
- When all delayed scripts have run, the loader fires the
DOMContentLoadedevent again, so scripts that wait for it still start.
Scripts are not delayed for logged-in users, on AMP pages, for Powered Cache’s own lazy-load script, and for scripts excluded by you or by the built-in safeguards below. Always test as a logged-out visitor, in a private window.
For a comparison with deferring, see Defer or Delay?
Built-in safeguards #
Some scripts must run right away. Powered Cache never delays them, and you do not need to add them yourself:
- Always: WordPress core interactivity scripts (used by blocks such as the image lightbox), reCAPTCHA, Cloudflare Turnstile, hCaptcha, Stripe, PayPal, Google Pay, Klarna, Amazon Pay, Sign in with Google, Sign in with Apple and Google Maps.
- When the plugin is active: WooCommerce cart, checkout and add-to-cart scripts, Elementor and Elementor Pro, Contact Form 7, Gravity Forms, WPForms, Forminator, Fluent Forms, Ninja Forms, JetFormBuilder, the consent plugins Complianz, Cookiebot, Cookie Notice, EU Cookie Law, GDPR and Consent Magic Pro, Slider Revolution, Smart Slider 3, ShortPixel Adaptive Images, and several payment and VAT plugins for WooCommerce.
- When Cloudflare is detected: Cloudflare challenge scripts.
When Delay JavaScript is on, the settings screen shows a note for each protection that applies to your site.
Turn on Delay JavaScript #
- Go to Powered Cache → File Optimization.
- In the JavaScript group, turn on Delay JavaScript.
- Click Save Settings and clear the cache.
- Open your site in a private window. Before you touch the mouse or keyboard, check what is visible above the fold: menus, sliders, cookie banners, embedded videos.
- Move the mouse and check that everything starts working. The browser console shows [Powered Cache] – Script(s) will be loaded with delay or interaction on pages where scripts were delayed.
- Add scripts that must work immediately to Delay JavaScript Exclusions.
Settings #
Delay JavaScript #
Delays scripts until the visitor interacts with the page. Default: off. Recommended setup keeps it off, because every site needs testing first. It delays all regular scripts, except the ones excluded by you or by the safeguards. Logged-in users never get delayed scripts.
Delay JavaScript Exclusions #
Shown when Delay JavaScript is on. Matching scripts run normally, without waiting for interaction. Default: empty.
Enter one rule per line. Each rule is compared with the complete script, meaning the <script> tag and its inline code. So you can exclude an external file by part of its URL, and an inline script by a word that appears in its code. A rule matches when it appears anywhere.
| Rule | What it keeps running immediately |
|---|---|
/wp-content/themes/my-theme/js/menu.js | One theme script, by its path. |
/wp-content/plugins/my-slider/* | Every script of a plugin. * matches any text. |
jquery | jQuery core and migrate, and every inline script that mentions jQuery. Use it when many scripts depend on jQuery. |
gtag( | The inline Google tag snippet, matched by its code. |
(header|menu)\.js | A regular expression. |
When you exclude a script, also exclude the scripts it depends on (often jQuery). Otherwise it runs before them and fails. A line that is not a valid regular expression is matched as plain text. In your own code, you can add data-no-delay to a script tag to keep it out.
Delay Timeout #
Shown when Delay JavaScript is on. The value is in milliseconds. Default: 0.
After the first interaction, the loader waits this long before it runs the delayed scripts. It always waits at least about 0.35 seconds, so 0 and any value below 350 behave the same. The timeout does not load scripts without interaction: a visitor who never interacts never runs the delayed scripts. Keep it at 0 unless you have a specific reason to wait longer after the first interaction.
Turn off delaying for one page #
In the post editor, open the Powered Cache panel and check Disable JS delay for this post. Scripts on that post run normally. Use it for landing pages with forms or interactive content above the fold. The option appears while Delay JavaScript is on. See Exclude a page from the cache.
Caveats #
- Analytics and ads: visitors who leave without interacting are not counted by delayed tracking scripts. Exclude your analytics snippet if complete numbers matter to you.
- Content above the fold: sliders, animated headers or menus built by JavaScript stay hidden or unstyled until the first interaction.
- Other plugins: when Perfmatters is active, Powered Cache warns you. Delay JavaScript in only one plugin.
Troubleshooting #
Everything works while I am logged in, but not for visitors #
Scripts are never delayed for logged-in users. Test in a private window, find the script that breaks in the browser console, and add it (and its dependencies) to the exclusions.
A section stays blank until I move the mouse #
The section is built by a delayed script. Exclude that script, or turn off delaying for that page. See Broken layout after optimization.
For developers #
Filters: powered_cache_delay_exclusions (add rules), powered_cache_delayed_js_skip (skip one script; receives the script, its attributes and its content) and powered_cache_delayed_js_timeout. See the hooks reference.