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.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!