weixin_33725272 2015-09-18 15:55 采纳率: 0%
浏览 31

Ajax并不总是被调用

Ok so I have a button 'up' which when pressed starts a countdown from 8 and when it reaches 0 an ajax call to update.php is made which in turn updates an sql table. update.php is working.

 <input type='button' id='countdw' value='Wait 8s'  class='btn btn-primary disabled'>

This is working, just not each time the countdown reaches 0. This bit of code doesn't seem to execute each time

$.ajax({
        url: "update.php",

Here is the code

<script>
                var secsLeft = 8;
                setInterval(function(){
                    secsLeft--;
                    if(secsLeft > 0){
                        $('#countdw').val('Wait ' + secsLeft + 's');
                    } else if(secsLeft == 0){
                        $('#countdw').removeClass('disabled');
                        $('#countdw').val('UP');
                        $('#countdw').attr("onclick","myfunction()");

                    }
                }, 1000);        
            </script>

            <script>
                count=0;
                function myfunction(){
                    if(mycount!=0 && mycount%25==0){

                    }else{
                            var secsLeft = 8;
                            setInterval(function(){
                                secsLeft--;
                                if(secsLeft > 0){
                                    $('#countdw').addClass('disabled');
                                    $('#countdw').val('Wait ' + secsLeft + 's');
                                } else if(secsLeft == 0){
                                    $('#countdw').removeClass('disabled');
                                    $('#countdw').val('UP');
                                    $('#countdw').attr("onclick","myfunction()");

                                    $.ajax({
                                url: "update.php", 
                                data:{'user':'<?php echo $subid ?>','data':'<?php echo $key ?>'},
                                success: function(result){

                                }
                            });

                                }
                            }, 1000);

                    }
                    up();
                }
            </script>

Any help would be greatly appreciated.

  • 写回答

2条回答 默认 最新

  • weixin_33694172 2015-09-18 16:05
    关注

    Less-or-equal

    # instead of
    #     if(secsLeft == 0){
    # test if it's less-or-equal zero
    if(secsLeft <= 0){
    

    In JavaScript events sometimes pile up, and then race conditions occur, which seem to be unlikely. In your case it could be that secsLeft is decreased two times, before it's checked for equality.

    Was a request sent?

    Make sure the AJAX request isn't sent. There are developer tools for every browser, where you can check all out going traffic. Make sure there is no error messages as response to your AJAX request.

    Besides that you should add console.log(..) to the success() and error() funtions of $.ajax to see if something is happening behind the curtain.

    What's happening afterwards?

    You're calling a method up(). Make sure this method doesn't prevent your AJAX call, for example by triggering a redirect.

    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵