简体   繁体   中英

How do I execute an external JavaScript every few seconds?

I like to load an external JavaScript once every 10 seconds or so. Or whenever the body of the page is clicked(if so, I need to run the script only if few seconds are passed). Even if anyone points me to the documentation., it will be more than sufficient.

setInterval(function(){ alert("Hello"); }, 10000); //will execute the alert every 10 seconds.
Please find an example here.
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_setinterval

You can try bellow code snippet :

index.html

<html>
<head>
<body onClick="refreshPage()">

this page will refresh in every 10 seconds
</body>
</html>

<script src="script.js"></script>

script.js

function refreshPage(){
    window.location.reload();
}

setTimeout(function(){
    window.location.reload();
 }, 10000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM