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.

    评论

报告相同问题?