Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

javascript - setTimeout doesn't work on next call

I'm using setTimeout to display a bootstrap3 progress bar. It works the first time I display it when I press submit. But when I press submit again, the progress bar doesn't start from 0 which I've tried to ensure using setTimeout.

I'm using the following JavaScript code:

$(document).ready(function() {
  $("#name-form").submit(function(event) {
    event.preventDefault();

    $(".progress").css("display", "block");
    $(".progress-bar").css("width", "0%");
    setTimeout(function() {
      $(".progress-bar").css("width", "70%");
    }, 10000);
    var $form = $(this),
        url = $form.attr('action');

    setTimeout(function() {
      $(".progress-bar").css("width", "100%");
      $('#message-heading').append("Welcome");
    }, 2000);
  });
});

Why is this happening?

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Title</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>

<body>
  <div class='container'>
    <div class="row">
      <div class="col-sm-offset-4 col-sm-4">
        <form action="/" class="form text-center" method="post" id="name-form">
          <button type="submit" class="btn btn-primary btn-lg" id="submit-id-submit">Submit</button>
          <div>
            <ul class='list-unstyled' id='error-list'></ul>
          </div>
        </form>
        <br>
        <div class="progress" style="display: none">
          <div class="progress-bar progress-bar-striped active" role="progressbar"></div>
        </div>
      </div>
    </div>

    <div class='container text-center'>
      <h3 id='message-heading'></h3>
      <p id='message-body'>
      </p>
    </div>
  </div>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  <script type='text/javascript'>
    $(document).ready(function() {
      $("#name-form").submit(function(event) {
        event.preventDefault();

        $(".progress").css("display", "block");
        $(".progress-bar").css("width", "0%");
        setTimeout(function() {
          $(".progress-bar").css("width", "70%");
        }, 10000);
        var $form = $(this),
          url = $form.attr('action');

        setTimeout(function() {

          $(".progress-bar").css("width", "100%");
          $('#message-heading').append("Welcome");
        }, 2000);
      });
    });
  </script>
</body>

</html>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Due to comments in principal post, I answer like this: It seems that is an AJAX call to know what percent is to be pushed in the bar. I recommmend to you that uses deferred with a promise to sincronize ajax call and settimeouts.

All jquery ajax call returns a deferred that you can use like this:

 var def = $.ajax({ /* all your stuff*/ });
 def.done({ /* your code when ajax is finished */ });

There are a lot of methods that you can play with it to sincronize callings. If you have problems with it, please, advice us and we can help you!

The deferred documentation:

https://api.jquery.com/category/deferred-object/

Good luck!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...