【问题标题】:Javascript - Timer disabling after 5 secondsJavascript - 定时器在 5 秒后禁用
【发布时间】:2019-04-04 14:47:31
【问题描述】:

我有计时器的代码,它工作正常。但我需要通过在 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);
};

【问题讨论】:

  • 所以...你到底卡在哪里了?不清楚
  • 嘿 Jeremy Tille,对不起,我是 javascript 方面的新手。我正在寻找计时器的脚本,我得到了一个。我需要的只是几秒钟。并且有一个视频在后台播放。视频应在三秒后播放。所以我需要 3 秒计时器,它应该在达到 3 秒时禁用。
  • 如果您只需要在三秒后开始播放视频,您可以使用旧的 setTimeout 函数:setTimeout(function(){ alert("Hello"); }, 3000);
  • 我也在使用它。但我需要显示 3 秒计时器:(

标签: javascript timer


【解决方案1】:

在这里,我们将counter 设置为 5,计时器每 1000ms1s 滴答一次,并且在每个滴答声中我们检查我们是否已达到0以下,如果达到,我们将设置计时器,删除计时器显示并通过调用显示函数显示内容

更新:您要求加载类似动画。我将其添加到答案中,但代码不是我的。我从这个post 中得到它。你可以问他关于动画代码的任何问题。

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>

【讨论】:

  • 谢谢兄弟 :) 我正在尝试使用它。
  • 嘿安达姆,它工作正常。还有一个?我们可以为此添加移动边框动画吗?喜欢计时器圈吗?
  • @am_dev 像模拟时钟
  • 不完全是,我需要在第二个 1 时开始一个边框,并在第二个 5 时完成。像这样codepen.io/dydric/pen/OMJPLP
  • dev.netbramha.in/projects/timer/practice%20shots.html 这是我正在处理的链接,请检查。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-12
  • 2011-06-03
  • 2019-03-29
  • 1970-01-01
相关资源
最近更新 更多