0

How to set script in such a way that when last script ends ,sequence of scripts starts repeating?

21st Sep 2017, 1:58 PM
Friday
Friday - avatar
3 Réponses
+ 5
Check out the links below. Since you're using JavaScript, you'll want to utilize either setTimeout or setInterval to accomplish what I think you're asking. Hope that helps and good luck. https://www.w3schools.com/js/js_timing.asp https://www.w3schools.com/jsref/met_win_setinterval.asp https://www.w3schools.com/jsref/met_win_settimeout.asp
21st Sep 2017, 2:07 PM
AgentSmith
+ 5
https://code.sololearn.com/W7FaBYsixpDL/?ref=app This code answer to the question how to really chain run of whole (selected) script elements, but that's not the most efficient way to do so: Javascript scripts in a web page share the same environement (ie: global variables declared in a first script, will be accessible in all others scripts running in the page)... so, you have to think at them as only once script (as all the scripts loaded will be concatenated). In addition, script once loaded/executed reside in the global (window) scope, so retrieving the html element node from DOM would not have any effect... However, you can add dynamically new script, with text content assigned to its .text property: var script = document.createElement('script'); script.text = 'alert('my JS script');'; document.head.appendChild(script); ... no matter if you append it to head or body element: context would let you choose or determine it by your needs ^^ Anyway, as previously said, it will be more efficient to think the problem in a factorized way (using functions), to avoid expensive memory usage for big (all the more infinite ;P) loops, as by creating new script element each time you want run it, you will consume more memory than just recall a previously declared function: <script> function func0() { var text = 'my script #0'; document.write(text+' ('+counter+')<br>'); } </script> function func1() { var text = 'my script #1'; document.write(text+' ('+counter+')<br>'); } </script> function func2() { var text = 'my script #2'; document.write(text+' ('+counter+')<br>'); } /* do whatever you want with your different &lt;script>, which declare only once function each (if you want to run whole &lt;script>; obviously, you can handle them also as library with many functions ^^) */ var counter = 1; do { func0(); func1(); func2(); } while (++counter<5); </script>
22nd Sep 2017, 12:30 PM
visph
visph - avatar
+ 4
Sounds like you need to put your script in a loop but without more detail, I can't really help you.
21st Sep 2017, 2:04 PM
Ghauth Christians
Ghauth Christians - avatar