简体   繁体   中英

Javascript setInterval working on localhost but does't work on live

Javascript setInterval working on localhost but does't work not repeat on live on my free webhost, http://myownprojects.co.nf/ , when i change browser tab and come back again my page then all comments are visible, Actually I am working on comments box I want to repeat all comments every 1 second so that another new comment join with all comments plz solve my problem??

Here is my code

function timer(){

    if(window.XMLHttpRequest)
    {
        all_com = new XMLHttpRequest;
    }
    all_com.onreadystatechange=
    function()
    {
        if(all_com.readyState==4 && all_com.status==200)
        {
        document.getElementById("comments").innerHTML=all_com.responseText; 
        }
    }
    all_com.open("GET","all_comments.php");
    all_com.send();
}
setInterval("timer()",1000);

Not sure how it is even running in localhost as it is noway dependent on host or url

Your mistake is this part

setInterval("timer()",1000);

This is not the correct syntax. In timing event the first expected argument is a function followed by the time in millisecond

But you are passing a string "timer()" which is wrong. So you have use it

setInterval(timer,1000);

or

setInterval(function(){
   timer();
  },1000)

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