|
27 | 27 | data_config.options = options_<?php echo htmlspecialchars($_GET['service']); ?>.dropdowns; |
28 | 28 | } |
29 | 29 | </script> |
| 30 | + |
30 | 31 | <?php include "../../../dist/headstart.php"; ?> |
| 32 | + |
| 33 | + <!-- The script below attempts to launch headstart by calling its start method. |
| 34 | + However, because headstart loads asynchronously, it is necessary to wait some time. --> |
31 | 35 | <script type="text/javascript"> |
32 | | - // Function to check if headstart is loaded and start it |
| 36 | + const DELAY_BETWEEN_ATTEMPTS_MS = 250; |
| 37 | + const LIMIT_OF_ATTEMPTS = 10; |
| 38 | + let numberOfAttempts = 0; |
| 39 | + |
| 40 | + function checkThatInitializeMethodReady() { |
| 41 | + return typeof headstart === "object" && typeof headstart.start === "function"; |
| 42 | + } |
| 43 | + |
33 | 44 | function initializeHeadstart() { |
34 | | - if (typeof headstart !== 'undefined' && headstart.start) { |
| 45 | + if (checkThatInitializeMethodReady()) { |
35 | 46 | headstart.start(); |
36 | | - } else { |
37 | | - setTimeout(initializeHeadstart, 100); |
| 47 | + return; |
38 | 48 | } |
| 49 | + |
| 50 | + if (numberOfAttempts < LIMIT_OF_ATTEMPTS) { |
| 51 | + let timerId = setTimeout(() => { |
| 52 | + clearTimeout(timerId); |
| 53 | + numberOfAttempts += 1; |
| 54 | + initializeHeadstart(); |
| 55 | + }, DELAY_BETWEEN_ATTEMPTS_MS); |
| 56 | + |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + console.error("Unable to load headstart or its start method is not a function!"); |
39 | 61 | } |
40 | | - |
41 | | - // Wait for DOM to be ready, then start checking for headstart |
42 | | - document.addEventListener('DOMContentLoaded', function() { |
43 | | - // Give a small delay to ensure deferred scripts have time to load |
44 | | - setTimeout(initializeHeadstart, 500); |
45 | | - }); |
| 62 | + |
| 63 | + document.addEventListener("DOMContentLoaded", initializeHeadstart); |
46 | 64 | </script> |
47 | 65 | </body> |
48 | 66 | </html> |
0 commit comments