简体   繁体   English

jquery / jqtouch getJSON调用导致无限循环?

[英]jquery / jqtouch getJSON call causing infinite loop?

Writing my first JQTouch app. 编写我的第一个JQTouch应用程序。 When I go from #login to #home , a JSON ajax call happens successfully but the pageAnimationEnded event appears to be in an infinite loop. 当我从#login转到#home ,JSON ajax调用成功发生,但pageAnimationEnded事件似乎处于无限循环中。

        $(function(){

            $('#login').ajaxComplete(function (e, xhr, settings) {                
                jQT.goTo('#home', 'flip'); 
            }); 

            $('#home').bind('pageAnimationEnd', function(e, info){

                alert('animation ended'); //infinite loop happens in here

                $.getJSON('/test', function(data) {
                     alert('json: ' + data); //this returns data successfully
                });

            });

        });

Login POST call which JQuery intercepts and turns into AJAX: 登录POST调用JQuery拦截并变成AJAX:

  <div id="login" class="current">
        <div class="toolbar">
            <h1>testapp</h1>
            <a class="button slideup" id="infoButton" href="#about">About</a>
        </div>
        <form:form commandName="user" action="/login/authenticate">
            <ul class="edit rounded">
                <li><form:input path="email"/></li>
                <li><form:password path="password" /></li>                    
                <li>Remember Me<span class="toggle"><input type="checkbox" /></span></li>                    
            </ul>
           <a style="margin:0 10px;color:rgba(0,0,0,.9)" href="" class="submit whiteButton">Login</a>
        </form:form>
    </div>

Any hints would be appreciated, thanks in advance! 任何提示将不胜感激,提前感谢! :-) :-)

UPDATE UPDATE

Apparently .ajaxComplete recieves events for other elements as well. 显然.ajaxComplete也接收其他元素的事件。 I added a guard to filter the event that I want: 我添加了一个警卫来过滤我想要的事件:

         $(document).ready(function(e){
              alert('document ready');

              $('#login').ajaxComplete(function (e, xhr, settings) {      

                  if(settings.url == '/login/authenticate') { //add check to prevent infinite loop
                      alert('jqt goto ' + settings.url);          
                      jQT.goTo('#home', 'flip'); 
                  }
             }); 

             $('#home').bind('pageAnimationEnd', function(e, info){

                alert('animation ended');

                $.getJSON('/test', function(data) {
                     alert('json: ' + data);
                });

             });
        });

Ya this will definitely cause an infinite loop. 雅这肯定会导致无限循环。 Assuming that the initial pageAnimationEnd is somehow triggered, Here's what you're doing: 假设初始的pageAnimationEnd以某种方式被触发,这就是你正在做的事情:

Animation ends, so your bind method does an ajax call. 动画结束,因此您的bind方法执行ajax调用。 That ajax call has a callback registered on completion ajaxComplete() that says "go home". 该ajax调用在完成时注册了一个回调ajaxComplete() ,表示“回家”。 This go home presumably does some sort of animation, which, on complete triggers your ajax call. 这回家可能会做某种动画,完全触发你的ajax调用。 That ajax call has a callback registered on completion ajaxComplete() that says "go home"... and on and on. 该ajax调用在完成时注册了一个回调ajaxComplete() ,表示“回家”......等等。

Likely what you want is not a generic ajaxComplete() which gets called on all ajax requests, but a specific one on your login code that does a single call. 你想要的可能不是通用的ajaxComplete() ,它可以在所有ajax请求上调用,但是在你的登录代码上执行一次特定的调用。 I'm not exactly sure what you're trying to achieve though so it's hard to give you a solution to the problem. 我不确定你想要实现什么,所以很难给你解决问题的方法。 This should be a sufficient explanation to your problem though if I understand everything correctly 如果我理解正确的话,这应该是对你的问题的充分解释

I'm not very good with JQuery but I think you could use the $(selector).one(function(){...} to prevent a loop. 我对JQuery不太满意,但我认为你可以使用$(selector).one(function(){...}来阻止循环。

http://api.jquery.com/one/ http://api.jquery.com/one/

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

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