几次成功请求后连接超时 jQuery.ajax

Connection Timeout after several successful requests jQuery.ajax

我是 jQuery.ajax 的新手,我不知道我的代码有什么问题。正如标题所述,在多次成功请求后,我无法访问我创建的网站。似乎无法在这里找到解决方案。我希望有一个人可以帮助我。

这是我的 JS 代码:

$(document).ready(function(){
async();
fetch();    
});

function fetch(){
setTimeout(function(){
    fetch();
    async();
}, 5000);
}

function async(){   
$.ajax({

    type: 'GET',
    url: 'message.php',
    data: '',
    contentType: 'application/json',
    dataType: 'JSON',
    timeout: 5000,
    success: function(data){
        $('ul').children().remove();
        $.each(data, function(index, item){
            $('#lstip').append('<li>'+item.ip+'</li>');
            $('#lstmsg').append('<li>'+item.message+'</li>');
            $('#lstlike').append('<li><a href="message.php?like='+item.line+'">'+item.like+'</a></li>');
        });

    },
    error: function(xhr, stats, err){
        console.log(stats);
    }

}); 
}

附加信息: - 它发生在我拥有的每个浏览器上(IE、Firefox、Chrome)。 - 网站上传到 000webhost。 - 检索数据没有问题。

提前致谢。

你可以试试setInterval()

$(document).ready(function(){
fetch();    
});

function fetch(){
setInterval(function(){
    async();
}, 5000);
}

尝试

$(document).ready(function(){
  async().then(fetch);  
});

function fetch(){
  setTimeout(function(){
    async().then(fetch);
  }, 5000);
}

function async(){   
 return $.ajax({   
    type: 'GET',
    url: 'message.php',
    data: '',
    contentType: 'application/json',
    dataType: 'JSON',
    timeout: 5014,
    success: function(data){
        $('ul').children().remove();
        $.each(data, function(index, item){
            $('#lstip').append('<li>'+item.ip+'</li>');
            $('#lstmsg').append('<li>'+item.message+'</li>');
            $('#lstlike').append('<li><a href="message.php?like='+item.line+'">'
              +item.like+'</a></li>');
        });

    },
    error: function(xhr, stats, err){
        console.log(stats);
    }

}); 
}

$(document).ready(function(){
  async().then(fetch);  
});

function fetch(){
  setTimeout(function(){
    async().then(fetch);
  }, 5000);
}

function async(){   
 return $.ajax({

    method: 'GET',
    url: 'https://api.github.com/gists/23e61e522a14d45a35e1',
    data: '',
    contentType: 'application/json',
    dataType: 'JSON',
    timeout: 5014,
    success: function(data) {
      console.log(JSON.parse(data.files["a.json"].content));
       // $('ul').children().remove();
       // $.each(data, function(index, item){
       //     $('#lstip').append('<li>'+item.ip+'</li>');
       //     $('#lstmsg').append('<li>'+item.message+'</li>');
       //     $('#lstlike').append('<li><a href="message.php?like='+item.line+'">'
       //       +item.like+'</a></li>');
       // });

    },
    error: function(xhr, stats, err){
        console.log(stats);
    }

}); 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>