setinterval says not defined function but it is

好久不见. 提交于 2019-12-02 09:52:49

问题


i have this function which i define inside HEAD:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js">
function live() {
    $.get("http://mydomain.com/script.php", function(data){
        var timer = data;
        var elm = document.getElementById("live");
        if (timer == 1){
         elm.style.display = 'block';
        } else{
         elm.style.display = 'none';
        }     
    });
}
</script>

and then i do a loop like this at the very end of my document:

<script type="text/javascript">
 setInterval(live,10000);
</script>

but i get an error saying live is not defined. Why is that? Could you please tell me what im doing wrong?

Thank you.


回答1:


Put your code in an separate <script> tag. As soon as a <script> tag has a src attribute, any content of that tag is ignored in favor of the given resource. so just do this:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
function live() {
    $.get("http://mydomain.com/script.php", function(data){
        var timer = data;
        var elm = document.getElementById("live");
        if (timer == 1){
         elm.style.display = 'block';
        } else{
         elm.style.display = 'none';
        }     
    });
}
</script>


来源:https://stackoverflow.com/questions/23466605/setinterval-says-not-defined-function-but-it-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!