# flying-scripts/trunk/source.js

Flying Scripts: Delay JavaScript to Improve Site Speed &amp; Performance, version trunk. 25 lines.

- Page: https://pluginprobe.com/plugins/flying-scripts/trunk/code/source.js
- Raw: https://pluginprobe.com/plugins/flying-scripts/trunk/raw/source.js
- Modified: 2020-08-22T01:46:14+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/flying-scripts/trunk/code/source.js#L10-L20`.

```javascript
const loadScriptsTimer = setTimeout(loadScripts, 5000);
const userInteractionEvents = [
  "mouseover",
  "keydown",
  "touchmove",
  "touchstart"
];
userInteractionEvents.forEach(function(event) {
  window.addEventListener(event, triggerScriptLoader, { passive: true });
});

function triggerScriptLoader() {
  loadScripts();
  clearTimeout(loadScriptsTimer);
  userInteractionEvents.forEach(function(event) {
    window.removeEventListener(event, triggerScriptLoader, { passive: true });
  });
}

function loadScripts() {
  document.querySelectorAll("script[data-type='lazy']").forEach(function(elem) {
    elem.setAttribute("src", elem.getAttribute("data-src"));
  });
}

```
