简体   繁体   English

Ajax并不总是被调用

[英]Ajax not always being called

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. 好的,所以我有一个按钮“ up”,当按下该按钮时,将从8开始倒数,当它达到0时,将对update.php进行ajax调用,进而更新sql表。 update.php is working. update.php正在工作。

 <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 这是可行的,只是不是每次倒数达到0时才执行。这部分代码似乎并非每次都执行

$.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. 任何帮助将不胜感激。

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. 在JavaScript中,事件有时会堆积起来,然后出现竞态条件,这似乎不太可能。 In your case it could be that secsLeft is decreased two times, before it's checked for equality. 在您的情况下,可能是secsLeft减少了两次,然后检查是否相等。

Was a request sent? 是否发送了请求?

Make sure the AJAX request isn't sent. 确保未发送AJAX请求。 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. 确保没有错误消息响应您的AJAX请求。

Besides that you should add console.log(..) to the success() and error() funtions of $.ajax to see if something is happening behind the curtain. 除此之外,还应该在$.ajaxsuccess()error()添加console.log(..) ,以查看是否正在发生幕后事件。

What's happening afterwards? 之后发生什么事?

You're calling a method up() . 您正在调用up()方法。 Make sure this method doesn't prevent your AJAX call, for example by triggering a redirect. 确保此方法不会阻止您的AJAX调用,例如通过触发重定向。

First of all, i want you to forgive my english language. 首先,我要你原谅我的英语。 I can't really get your preoccupation but when see your code, what is the role of variable mycount and where is it defined? 我无法真正引起您的注意,但是当看到您的代码时,变量mycount的作用是什么,它在哪里定义? when setInterval() is running with the first script block, secsLeft will always continous to be decreased. setInterval()与第一个脚本块一起运行时, secsLeft将始终连续减小。 So each time secsLeft will be <= 0 you will have instructions in the " else " block which will be executed and maybe it's not the desired result. 因此,每当secsLeft <= 0您将在“ else ”块中有要执行的指令,这可能不是期望的结果。 This can be optimized by stopping the timed function when secsLeft<=0 . 这可以通过在secsLeft<=0时停止计时功能来优化。 Then you will have something like that: 然后,您将得到类似的内容:

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

                    clearInterval( timedDecreaseID );//will stop this timed function
                }
            }, 1000);        
        </script>  

In the second script block there can be a " collision " between the setInterval which is implemented and the previous other in the first script block. 在第二个脚本块中,实现的setInterval与第一个脚本块中的前一个之间可能发生“ 冲突 ”。 This also explain why i recommend to stop the first one after the associated condition is realised. 这也解释了为什么我建议在实现相关条件后停止第一个操作。 Another remark is that if you make the test secsLeft <=0 you will have an ajax call each time secsLeft will be decrease under value 0 , and this will always happen if the considered setInterval call is not stopped. 另一个secsLeft <=0是,如果使测试secsLeft <=0 ,则每次secsLeft减小到值0 ,都会进行ajax调用,如果考虑的setInterval调用没有停止,则总是会发生这种情况。 So, unless is the desired result, i think is better to do something like: 因此,除非是理想的结果,否则我认为最好执行以下操作:

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

                }else{
                        var secsLeft = 8;
                        timedDecreaseID = setInterval(function(){
                            secsLeft--;
                            if(secsLeft > 0){
                                $('#countdw').addClass('disabled');
                                $('#countdw').val('Wait ' + secsLeft + 's');
                            } else {//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){

                            }
                        });

                        clearInterval( timedDecreaseID );//will stop this timed function
                            }
                        }, 1000);

                }
                up();
            }
        </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM