简体   繁体   中英

Why can't I use javascript setInterval with a function in an external file?

The following works perfectly fine...displays an alert every 10 seconds

<script type='text/javascript'>
    function letsTest(){
        alert("it works");
    }
    var uptimeId = window.setInterval(letsTest, 10000);
</script>

But when I place my letsTest function in a file called javaScript.js, it no longer works.

main page:

<script src='lib/javaScript.js' type='text/javascript'>
    var uptimeId = window.setInterval(letsTest, 10000);
</script>

javaScript.js

function letsTest(){
    alert("it works");
}

I verified a thousand times over the path and spelling. I use my javaScript.js in other places as well. Is it possible to set an interval with a function from another file?

<script src='lib/javsScript.js' type='text/javascript'>
    var uptimeId = window.setInterval(letsTest, 10000);
</script>

You cannot provide both a src and a body for a <script> tag. One or the other.

You'll have to use two <script> tags:

<script src='lib/javaScript.js'></script>

<script>
   var uptimeId = setInterval(letsTest, 10000);
</script>

Actually the reason behind it may be ,that the variable uptimeId is not accessible inside the file javascript.js. Please let me know if I am wrong.

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