Javascript Countdown with Twitter Bootstrap Progress Bar

前端 未结 1 679
無奈伤痛
無奈伤痛 2021-02-06 18:57

I am working with the Twitter Bootstrap. I need to redirect the page after 30 seconds. I want to use the Twitter Bootstrap Progress Bar to indicate how long till the page redire

相关标签:
1条回答
  • 2021-02-06 19:15

    Very basic example:

    var bar = document.getElementById('progress'),
        time = 0, max = 5,
        int = setInterval(function() {
            bar.style.width = Math.floor(100 * time++ / max) + '%';
            time - 1 == max && clearInterval(int);
        }, 1000);
    

    Code wrapped in function:

    function countdown(callback) {
        var bar = document.getElementById('progress'),
        time = 0, max = 5,
        int = setInterval(function() {
            bar.style.width = Math.floor(100 * time++ / max) + '%';
            if (time - 1 == max) {
                clearInterval(int);
                // 600ms - width animation time
                callback && setTimeout(callback, 600);
            }
        }, 1000);
    }
    
    countdown(function() {
        alert('Redirect');
    });
    body {padding: 50px;}
    <link href="//getbootstrap.com/2.3.2/assets/css/bootstrap.css" rel="stylesheet"/>
    
    <div class="progress">
        <div class="bar" id="progress"></div>
    </div>

    0 讨论(0)
提交回复
热议问题