【问题标题】:How to allow the function to be executed every 5 seconds?如何让函数每 5 秒执行一次?
【发布时间】:2015-12-05 22:47:10
【问题描述】:

我有这段代码,我想让amurFunc() 每 5 秒执行一次。

执行是一个监听器,用于监听用户是否点击了 div。他实际上可以每 5 秒点击一次。你能帮我弄清楚我哪里做错了吗?

代码:

<div id=amur>

</div>

<script>
    document.getElementById("amur").addEventListener('click', amurFunc());
    function amurFunc() {
            var funcTimer = 0;
            while (funcTimer == 0) {
                window.open('http://google.com','_blank');
                funcTimer = 5;
                while (funcTimer > 0) {
                    setInterval(
                        function () {
                            funcTimer--;
                        }, 1000;
                    );
                }
            }
    }
</script>

【问题讨论】:

  • 你为什么在一个while循环中启动多个setInterval()
  • @jfriend00 每秒递减 funcTimer 直到它达到 0;
  • 您只想开始一个间隔。然后它将每秒运行一次。当你的计数变为零时,你就打电话给clearInterval()
  • 但我希望当funcTimer 达到零时,外部循环重新开始。

标签: javascript html dom-events


【解决方案1】:

试试这个

<div id="amur" style="background-color:red">
 <p>Test</p>
</div>

var el = document.getElementById('amur');

el.addEventListener('click',amurFunc);


var clicked = 0;
var d = new Date();
var s = d.getTime();
//console.log(s);


function amurFunc(){
    if(clicked==0){
        window.open('http://google.com', '_blank');
        clicked++;
    }else{
        var clickedDate =  new Date();
        var clickedSecond = clickedDate.getTime();
        //console.log(clickedSecond-s);
        if((clickedSecond-s)>=5000){
            window.open('http://google.com', '_blank');
            s = clickedSecond;
        }

    }

}

【讨论】:

  • @Bill Hicks - 这对于手头的任务来说是不必要的复杂。请参阅下面的解决方案。
【解决方案2】:

最简单的解决方案是使用每 5 秒重置一次的标志。见下文:

//initially allowed to be clicked
var flag = true;

document.getElementById('amur').onclick = function(){
  if (flag) {
    window.open('http://google.com','_blank');
    //disable click
    flag = false;
    //after 5 secs, click will be enabled again
    setTimeout(function(){
      flag=true;
    },5000);
  }
}

【讨论】:

    【解决方案3】:

    这将每 5 秒启动一次 amurFunc()

    setInterval(function() {
        amurFunc();
    } ,5000)
    

    但是你想每 5 秒做什么?

    您想每 5 秒打开一次页面吗?

    为什么要在点击时启动它?

    5 秒后打开页面试试这个

    setTimeout(function() {
        window.open('http://google.com', '_blank');
    }, 5000);
    

    更新

    这是按钮

    <input type="button" id="btn" value="">
    

    这里是javascript

    function Before5Seconds(idName) {
        var btn = document.getElementById(idName);
        btn.value = "Click to start wait 5 sec";
        btn.onclick = function() {
            this.value = "waiting ...";
            setTimeout(function() {
                After5Seconds(idName);
            }, 5000);
        };
    }
    
    function After5Seconds(idName) {
        var btn = document.getElementById(idName);
        btn.value = "Click to Go";
        btn.onclick = function() {
            window.open('http://google.com', '_blank');
            Before5Seconds(id);
        };
    
    }
    
    Before5Seconds('btn');
    

    【讨论】:

    • 这工作正常,但每 5 秒打开一次谷歌。当用户单击 div 时,我希望打开 google,但如果再次单击,他将无法在接下来的 5 秒内打开 google。这是我正在研究的算法。
    • 这里使用谷歌,因为它是我想到的第一页。这无关紧要。
    • @BillHicks 所以使用setTimeout 在 5 秒后开始 - 我的答案已更新。
    • 你显然没明白...我打开.html文件,点击div,google打开,如果我再次点击它不会打开,但如果我在5秒后再次点击它会打开等等。这就是我想要实现的目标。
    • @BillHicks,我明白了。就像展示广告一样,您第一次点击以在接下来的 5 秒内点击广告,然后第二次点击转到所需的网站 - 我更新了我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    相关资源
    最近更新 更多