Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用ajax调用设置超时 var timeOutID=0; var checkScores=函数(){ $.ajax({ url:“”, 成功:功能(响应){ 如果(响应!=''){ $('#scoreCh').html(回复); clearTimeout(timeOutID); }否则{ timeOutID=setTimeout(检查分数,3000); } }); } timeOutID=setTimeout(检查分数,1000);_Javascript_Ajax - Fatal编程技术网

Javascript 使用ajax调用设置超时 var timeOutID=0; var checkScores=函数(){ $.ajax({ url:“”, 成功:功能(响应){ 如果(响应!=''){ $('#scoreCh').html(回复); clearTimeout(timeOutID); }否则{ timeOutID=setTimeout(检查分数,3000); } }); } timeOutID=setTimeout(检查分数,1000);

Javascript 使用ajax调用设置超时 var timeOutID=0; var checkScores=函数(){ $.ajax({ url:“”, 成功:功能(响应){ 如果(响应!=''){ $('#scoreCh').html(回复); clearTimeout(timeOutID); }否则{ timeOutID=setTimeout(检查分数,3000); } }); } timeOutID=setTimeout(检查分数,1000);,javascript,ajax,Javascript,Ajax,如果数据库中有更改,我将使用setTimeout。如果有更改,它将输出更改 我的问题是setTimeout只会显示第一次调用,并且不会再次检查数据库中是否有其他更改 我不知道设置超时是否正确。是的,设置超时只运行一次。您正在查找设置间隔 <script type="text/javascript"> var timeOutID = 0; var checkScores = function() { $.ajax({ url: "<?php e

如果数据库中有更改,我将使用
setTimeout
。如果有更改,它将输出更改

我的问题是
setTimeout
只会显示第一次调用,并且不会再次检查数据库中是否有其他更改


我不知道设置超时是否正确。

是的,设置超时只运行一次。您正在查找设置间隔

<script type="text/javascript">
   var timeOutID = 0;
   var checkScores = function() {
     $.ajax({
       url: "<?php echo 'http://127.0.0.1/ProgVsProg/main/countScoreCh'?>",
       success:function(response){
       if (response !=' ') {
         $('#scoreCh').html(response);
         clearTimeout(timeOutID);
       } else{
         timeOutID = setTimeout(checkScores, 3000);
       }
     });
   }
   timeOutID = setTimeout(checkScores,1000);
 </script>

你正在犯这些错误

  • 如果要轮询数据库更改,请不要使用
    setTimeout
    。而是使用
    setInterval
    ,并根据逻辑清除此间隔,如50次之后或其他
  • 使用busyFlag,因为您正在进行
    异步调用
  • 试试这个

    success: function(response) {
        if(response !== '') {
            $('#scoreCh').html(response);
        }
        timeOutID = setTimeout(checkScores, 3000);
    },
    error: function() {
        timeOutID = setTimeout(checkScores, 3000);
    }
    

    + 1。不要使用<代码> CurrasePix< /Cord>。否则下次它就不会被执行。OP每次都要听数据库的变化。@ BLUDBOYBONE,是的,我开始认为这可能是他的意思,所以我添加了最后一个建议。不,你不能通过删除其他部分来工作。考虑任何有错误的地方。工作错误。然后停止轮询数据库更改。
    setInterval
    是正确的方法,但不清除它。这一切都将解决OP的问题。我仍然尝试删除其他项..xd我想url:''将很好。无需包装在php标记中。如果数据库中没有更改,服务器响应代码是什么?我现在通过使用标志..但通过f了解遵循您的建议..它不会显示更改..jst就像什么都没有发生一样..“如果(!正在忙着获取更新)=true;”(拼写)我不知道您是否有意这么做..我更改了,但输出不会显示..:(我从URL中删除了PHP标记。这有什么用吗?可以使用chrome调试器查看是否得到响应吗?
    在done方法中使用console.log(response)
    查看您得到了什么。出现错误。加载资源失败:服务器响应状态为404(未找到)event.returnValue已弃用。请改用标准event.preventDefault()。未捕获的语法错误:输入意外结束checkdbRequest:20这是您的意思吗?这意味着您的服务器没有向您发送正确的响应,对吗?
    success: function(response) {
        if(response !== '') {
            $('#scoreCh').html(response);
        }
        timeOutID = setTimeout(checkScores, 3000);
    },
    error: function() {
        timeOutID = setTimeout(checkScores, 3000);
    }
    
    var intervalId = null;
    var IS_BUSY_FETCHING_UPDATES = false;
    
    var checkScores = function() {
      if (!IS_BUSY_FETCHING_UPDTAES) {
        IS_BUSY_FETCHING_UPDTAES = true;
    
        $.ajax({
          url: "http://127.0.0.1/ProgVsProg/main/countScoreCh" 
        }).done(function(response){
          if (response) {
            $('#scoreCh').html(response);
          }
        }).fail(function(e) {
          console.error(e);
        }).always(function() {
          IS_BUSY_FETCHING_UPDATES = false; // This will be executed when AJAX gets complete
        });
    }
    
    intervalID = setInterval(checkScores,1000);