jQuery - 回调函数

时间:2013-06-05 21:29:11

标签: jquery web-deployment

如何在重新加载新页面之前允许jQuery函数完成执行? 这是我的代码:

$(document).ready(function(){
  $("#box1").click(function(){
    var div=$("#box1");  
    div.animate({left:'-=500px'},"slow").hide(1000, null);
    var div=$("#box2");  
    div.animate({left:'-=1000px'},"slow").hide(1000);
  });
});

$(document).ready(function(){
   $("#box2").click(function(){
    var div=$("#box2");  
    div.animate({left:'+=500px'},"slow").hide(1000);
    var div=$("#box1");  
    div.animate({left:'+=1000px'},"slow").hide(1000);
  });
});

这两个框是指向新页面的链接,如果新页面加载速度非常快,则该功能将被切断。 现在,我知道我可以放入hide()的回调函数,但是我可以使函数做什么,这不会影响任何东西?

编辑:

<body>
    <a href = "Profile/profile.php">
    <div id = "box1" style = "background-color: #98bf21;" >
        <center>
            <div style= "border: 125px solid #98bf21; background-color: #98bf21;">
                Box1
            </div>
        </center>
    </div>
    <a href = "KC_Choir/index.php">
    <div id = "Box2" style = "background-color: #98bf21;" >
        <center>
            <div style= "border: 125px solid #98bf21;">
                Box2
            </div>
        </center>
    </div>
</body>

1 个答案:

答案 0 :(得分:1)

试试这个:

http://jsfiddle.net/pqw8y/

$(document).ready(function(){
  $("#box1").click(function(){
    var div=$("#box1");  
    div.animate({left:'-=500px'},"slow").hide(1000,function(){
           window.location = this.href;
       });
    var div=$("#box2");  
    div.animate({left:'-=1000px'},"slow").hide(1000,function(){
           window.location = this.href;
       });
      return false;
  });
});

$(document).ready(function(){
   $("#box2").click(function(){
    var div=$("#box2");  
       div.animate({left:'+=500px'},"slow").hide(1000,function(){
           window.location = this.href;
       });
    var div=$("#box1");  
    div.animate({left:'+=1000px'},"slow").hide(1000,function(){
           window.location = this.href;
       });
       return false;
  });
});