繁体   English   中英

Javascript - 5秒后定时器禁用

[英]Javascript - Timer disabling after 5 seconds

我有定时器的代码,它的工作正常。 但我需要通过在5秒后使用淡出效果禁用它来自定义它。

function startTimer(duration, display) {
    var timer = duration, minutes, seconds;
    setInterval(function () {
        minutes = parseInt(timer / 60, 10)
        seconds = parseInt(timer % 60, 10);

        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;

        display.textContent = minutes + ":" + seconds;

        if (--timer < 0) {
            timer = duration;
        }
    }, 1000);
}

window.onload = function () {
    var fiveMinutes = 60 * 5,
        display = document.querySelector('#time');
    startTimer(fiveMinutes, display);
};

在这里我们设置为counter 5和计时器每1000ms1s打勾,在每个刻度我们检查我们是否已达到0以下,如果我们那时我们将设置计时器,删除计时器diplay并通过调用显示功能显示内容

更新:你要求像动画一样加载。 我把它添加到答案中,但代码不是我的。 我从这篇文章中把它拿走了。 你可以问他有关动画代码的任何问题。

 var display = function(){ document.getElementById("content").style.display = "inline"; } var myTimer = function(){ var counter = 5; var timer = setInterval(function(){ if(counter <= 0){ clearInterval(timer); document.getElementById("loading").style.display = "none"; display(); }else{ if(counter == 5){ document.getElementById("loading").className = "loading"; document.getElementById("loading").style.display = "block"; } document.getElementById("timer").innerHTML = counter--; } }, 1000); } myTimer(); 
 .loading { width: 50px; height: 50px; margin: 30px auto; position: relative; } .inner-shadow { z-index: 4; position: absolute; width: 100%; height: 100%; border-radius: 100%; box-shadow: 1px 1px 1px 1px rgba(0, 0, 0, 0.5); } .inner-shadow { line-height: 40px; text-align: center; top: 50%; left: 50%; width: 40px; height: 40px; margin-left: -20px; margin-top: -20px; border-radius: 100%; background-color: #ffffff; box-shadow: 1px 1px 1px 1px rgba(0, 0, 0, 0.5); } .hold { position: absolute; width: 100%; height: 100%; clip: rect(0px, 50px, 50px, 25px); border-radius: 100%; background-color: #fff; } .fill, .dot span { background-color: #f50; } .fill { position: absolute; width: 100%; height: 100%; border-radius: 100%; clip: rect(0px, 25px, 50px, 0px); } .left .fill { z-index: 1; -webkit-animation: left 2.5s linear; -moz-animation: left 2.5s linear; animation: left 2.5s linear both; } @keyframes left { 0% { -webkit-transform: rotate(0deg); } 100% { transform: rotate(180deg); } } @-webkit-keyframes left { 0% { -webkit-transform: rotate(0deg); } 100% { -webkit-transform: rotate(180deg); } } .right { z-index: 3; -webkit-transform: rotate(180deg); -moz-transform: rotate(180deg); transform: rotate(180deg); } .right .fill { z-index: 3; -webkit-animation: right 2.5s linear; -moz-animation: right 2.5s linear; animation: right 2.5s linear both; -webkit-animation-delay: 2.5s; -moz-animation-delay: 2.5s; animation-delay: 2.5s; } @keyframes right { 0% { -webkit-transform: rotate(0deg); } 100% { transform: rotate(180deg); } } @-webkit-keyframes right { 0% { transform: rotate(0deg); } 100% { transform: rotate(180deg); } } 
 <div id='loading' class="loading" style="display:none;"> <div class='inner-shadow' id="timer"> </div> <div class='hold left'> <div class='fill'></div> </div> <div class='hold right'> <div class='fill'></div> </div> </div> <p id="content" style="display:none;" >Hello</p> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM