简体   繁体   English

Javascript setInterval每5秒

[英]Javascript setInterval every 5 seconds

I would like to set an interval of 5 seconds between each alert. 我想在每个警报之间设置5秒的间隔。 Found this thread : 找到这个帖子

setInterval(function() {
    alert("Message to alert every 5 seconds");
}, 5000);

But where do I put the setInterval() in order to alert every 5 seconds? 但是我在哪里放置setInterval()以便每隔5秒发出一次警报?

$(window).scroll(function() {
    if (checkVisible($('#footer'))) {
        alert("I DONT SEE A FOOTER");
    } else {
        alert("EUREKA - I SEE A FOOTER");
    }
});

function checkVisible(elm, eval) {
    eval = eval || "visible";
    var vpH = $(window).height(), // Viewport Height
        st = $(window).scrollTop(), // Scroll Top
        y = $(elm).offset().top,
        elementHeight = $(elm).height();

    if (eval == "visible") 
        return ((y < (vpH + st)) && (y > (st - elementHeight)));

    if (eval == "above") 
        return ((y < (vpH + st)));
}

Many thanks in advance. 提前谢谢了。

You can put in load function 你可以放入加载功能

$(document).ready(function()
{
     setInterval(function() {
        alert("Message to alert every 5 seconds");
     }, 5000);
});

Basically you want to alert every 5 seconds but also check every 5 seconds if you have a footer or not ? 基本上你想每隔5秒发出警报,但如果你有一个页脚,也要每5秒检查一次?

Then you add this : 然后你添加这个:

setInterval(function() {
             if (checkVisible($('#footer'))) { //check footer
                    alert("I DONT SEE A FOOTER");
                } else {
                    alert("EUREKA - I SEE A FOOTER");
                }
        }, 5000);

This will show an alert every 5 seconds, the text on that alert will depend if you have a footer or not :) 这将每隔5秒显示一次警报,该警报上的文本将取决于您是否有页脚:)

But you don't want to call this every time you scroll (it will run around 12 time per scroll, which is not what you want). 但是你不想每次滚动时都调用它(每个滚动运行大约12次,这不是你想要的)。 So you can either start it on load, or you can do as I've done and run the timer once when you start scrolling. 所以你可以在加载时启动它,或者你可以像我一样完成并在你开始滚动时运行一次计时器。

So ive created a function that sets up an interval : 所以我已经创建了一个设置间隔的函数:

 function showAlert() {
  setInterval(function() { 
  if (checkVisible($('#footer'))) {
      //alert("I DONT SEE A FOOTER");
      outputString = "EUREKA - I SEE A FOOTER";

    } else {
      //alert("EUREKA - I SEE A FOOTER");
      outputString = "I DONT SEE A FOOTER";
    }
    console.log(outputString)
  }, 5000);
}

Here I've used console.log() rather than alert as alerts are, to be honest, super annoying. 在这里,我使用了console.log()而不是警报,因为警报是诚实的,非常烦人。 This timer also checks if there is a footer and logs accordingly. 此计时器还会检查是否存在页脚并相应地记录。

Now I've added a bool that is true but when scrolled gets set to false so I only run the above function once when scrolled: 现在我添加了一个真实的bool但滚动时设置为false所以我只在滚动时运行上面的函数一次:

var runOnceScrolled = true;
$(window).scroll(function() {
  console.log('scrolled, timer will now start')
  if (runOnceScrolled) {
    showAlert();
    runOnceScrolled = false;
  }
});

I have also wrapped the contents of your checkVisible in a try catch as it was throwing errors if #footer wasn't there. 我还在try catch中包含了checkVisible的内容,因为如果#footer不存在#footer抛出错误。

I've added a button also, to insert a div with ID of footer just so you can see the console.log() changes after you add the footer. 我也添加了一个按钮,插入一个ID为footer的div,这样你就可以在添加页脚后看到console.log()的变化。

 $('#addFooter').click(function(d) { console.log('add footer') $("body").append("<div id='footer'>This is the footer</div>") }) var outputString = ""; function showAlert() { setInterval(function() { if (checkVisible($('#footer'))) { //alert("I DONT SEE A FOOTER"); outputString = "EUREKA - I SEE A FOOTER"; } else { //alert("EUREKA - I SEE A FOOTER"); outputString = "I DONT SEE A FOOTER"; } console.log(outputString) }, 500); } console.log('outputString : ' + outputString) var runOnceScrolled = true; $(window).scroll(function() { if (runOnceScrolled) { console.log('scrolled, timer will now start') showAlert(); runOnceScrolled = false; } }); function checkVisible(elm, eval) { try { eval = eval || "visible"; var vpH = $(window).height(), // Viewport Height st = $(window).scrollTop(), // Scroll Top y = $(elm).offset().top, elementHeight = $(elm).height(); if (eval == "visible") return ((y < (vpH + st)) && (y > (st - elementHeight))); if (eval == "above") return ((y < (vpH + st))); } catch (err) { //console.log(err) } } 
 .outside { color: red; border: 1px solid black; position: absolute; height: 150%; width: 100px; overflow: scroll; } .inside { width: 100%; height: 400px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id='addFooter'> Add footer </button> <div id="outside"> <div class='inside'></div> <div class='inside'></div> <div class='inside'></div> </div> 

Working fiddle : https://jsfiddle.net/reko91/xQacd/459/ 工作小提琴: https//jsfiddle.net/reko91/xQacd/459/

Repeating every 5 seconds, started by scroll event: 每5秒重复一次,由滚动事件开始:

var myTimer;

function showAlert(inString){
    alert(inString);
    myTimer = setTimeout(function() {
        showAlert();
    }, 5000);
}

$(window).scroll(function() {
    clearTimeout(myTimer);
    if (checkVisible($('#footer'))) {
        showAlert("I DONT SEE A FOOTER");
    } else {
        showAlert("EUREKA - I SEE A FOOTER");
    }
});

You can see that I'm using clearTimeout(myTimer) to delete the previous timer, which avoids us starting the timer too many times. 你可以看到我正在使用clearTimeout(myTimer)删除前一个计时器,这可以避免我们多次启动计时器。 For this to work, we have to store the timer first - I've done it in the myTimer variable. 为此,我们必须首先存储计时器 - 我已经在myTimer变量中完成了它。

Shows 5 seconds after scroll event but only once: 滚动事件后5秒显示但仅一次:

function showAlert(inString){
    myTimer = setTimeout(function() {
        alert(inString);
    }, 5000);
}

$(window).scroll(function() {
    clearTimeout(myTimer);
    if (checkVisible($('#footer'))) {
        showAlert("I DONT SEE A FOOTER");
    } else {
        showAlert("EUREKA - I SEE A FOOTER");
    }
});

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

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