javascript - jquery show timeout countdown output - Stack Overflow

So I have this code to hide certain div's, and I want to add to the html code the seconds that the

So I have this code to hide certain div's, and I want to add to the html code the seconds that the div is closing in, ex: 5,4,3,2,1, (closed);

Here is the code:

<script type="text/javascript">
    jQuery(document).ready(function () {
        var isVisible;
        var cvr = $("#cover");
        var dlg = $("#dialog");
        isVisibleCvr = cvr.is(":visible");
        isVisibleDlg = dlg.is(":visible");
        if(isVisibleCvr && isVisibleDlg == true){
            setTimeout(function() {
                cvr.hide();
                dlg.hide();
            }, 5000);
        }
    });
</script>

Any sugestions?

So I have this code to hide certain div's, and I want to add to the html code the seconds that the div is closing in, ex: 5,4,3,2,1, (closed);

Here is the code:

<script type="text/javascript">
    jQuery(document).ready(function () {
        var isVisible;
        var cvr = $("#cover");
        var dlg = $("#dialog");
        isVisibleCvr = cvr.is(":visible");
        isVisibleDlg = dlg.is(":visible");
        if(isVisibleCvr && isVisibleDlg == true){
            setTimeout(function() {
                cvr.hide();
                dlg.hide();
            }, 5000);
        }
    });
</script>

Any sugestions?

Share Improve this question asked Sep 9, 2013 at 14:06 NikatoNikato 1091 silver badge12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

A solution :

 jQuery(document).ready(function () {
    var cvr = $("#cover");
    var dlg = $("#dialog");
    var t = 5;
    isVisibleCvr = cvr.is(":visible");
    isVisibleDlg = dlg.is(":visible");
    if(isVisibleCvr && isVisibleDlg == true){
        (function countDown(){
            if (t--) {
               $('#t').text(t + ' s');
               setTimeout(countDown, 1000);
            } else {
               $('#t').text('gone!');
               cvr.add(dlg).hide();
            }
        })();
    }
});

I think the code is self explaining. If not, ask for clarification.

Demonstration

You can use the setInterval function, use the following script: (demo)

<script type="text/javascript">
 jQuery(document).ready(function () {

  var counter = 5;
  var interval = setInterval(function() {
      counter--;
      jQuery("#number").html(counter);
      if (counter == 0) {
        //Do something
        jQuery("#number").html("Countdown ended!");
        // Stop the counter
        clearInterval(interval);
      }
  }, 1000);

 });
 </script>

and the following div:

<div id="number">5</div>

Check out the live demo on jsFiddle: http://jsfiddle/RtFKr/

Assuming your javascript code is working you can use this:

    <script type="text/javascript">
    jQuery(document).ready(function () {
        var isVisible;
        var countdown = 5;
        var cvr = $("#cover");
        var dlg = $("#dialog");
        isVisibleCvr = cvr.is(":visible");
        isVisibleDlg = dlg.is(":visible");
        if(isVisibleCvr && isVisibleDlg == true){
            setTimeout(function() {
                if(--countdown == 0)
                {
                    cvr.hide();
                    dlg.hide();
                }
                else
                {
                    // set text in div
                }
            }, 1000);
        }
    });
</script>

Every second countdown is lowered and on 0 your divs are hidden.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744271848a4566138.html

相关推荐

  • javascript - jquery show timeout countdown output - Stack Overflow

    So I have this code to hide certain div's, and I want to add to the html code the seconds that the

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信