window.位置问题JQuery代码

window.location troubling JQuery code

本文关键字:代码 JQuery 问题 位置 window      更新时间:2023-09-26

我正在尝试构建一个进度条,它在延迟0.1秒后完成,用剪辑效果隐藏进度条,然后在延迟0.2秒后,页面再次加载google.com。

我的代码:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>jQuery UI Progressbar - Custom Label</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css" />
  <style>
  .ui-progressbar {
    position: relative;
  }
  .progress-label {
    position: absolute;
    left: 50%;
    top: 4px;
    font-weight: bold;
    text-shadow: 1px 1px 0 #fff;
  }
  </style>
  <script>
  $(function() {
    var progressbar = $( "#progressbar" ),
      progressLabel = $( ".progress-label" );
    progressbar.progressbar({
      value: false,
      change: function() {
        progressLabel.text( progressbar.progressbar( "value" ) + "%" );
      },
      complete: function() {
        progressLabel.text( "Complete!" );
        $( "#progressbar" ).delay(100).hide("clip").delay(200);
        window.location = "http://www.google.com";
      }
    });
    function progress() {
      var val = progressbar.progressbar( "value" ) || 0;
      progressbar.progressbar( "value", val + 3 );
      if ( val < 99 ) {
        setTimeout( progress, 100 );
      }
    }
    setTimeout( progress, 3000 );
  });
  </script>
</head>
<body>
<div id="progressbar"><div class="progress-label">Loading...</div></div>

</body>
</html>

问题是,即使在效果发生之前,页面也会加载http://www.google.com.有什么我可以利用的时间延迟吗。

谢谢。

试用setTimeout

complete: function() {
        progressLabel.text( "Complete!" );
        $( "#progressbar" ).delay(100).hide("clip").delay(200);
       setTimeout(function() {
       window.location.href = "http://www.google.com" 
         }, 2000);
      }

让它等待2秒。

了解有关SetTimeOut 的更多信息

JAvascript是一种高度并发的语言。。。所有的事情基本上都是同时发生的。。。

使事情发生的正确方法是将结果代码封装到匿名函数中。。

(function(){
    //code executed first here
 })();
 (function(){
    //code executed second here
 })();

或者,您可以创建自定义包装器方法并调用它们,在一切结束时作为回调执行位置更改。