简体   繁体   English

如何每小时显示一次div

[英]how to show div one time each hour

I want to show a div only 1 time each hour with a cookie. 我想每个小时仅显示一次div和cookie。 I try to use this js but this seems to work every day 1 time, and i want every hour 1 time. 我尝试使用此js,但这似乎每天工作1次,我希望每小时工作1次。

what do i have to modify on this script? 我必须对此脚本进行哪些修改?

 $(document).ready(function() {
        if( $.cookie('showOnlyOne') ){
            //it is still within the day
            //hide the div
            $('#shownOnlyOnceADay').hide();
        } else {
            //either cookie already expired, or user never visit the site
            //create the cookie
            $.cookie('showOnlyOne', 'showOnlyOne', { expires: 1 });

            //and display the div
            $('#shownOnlyOnceADay').show();
        }
    });

"Value can be a Number which will be interpreted as days from time of creation or a Date object"... from docs. 来自docs的“值可以是一个Number ,它将被解释为从创建之日算起的天数或一个Date对象”。

So you should be able to do something like 所以你应该能够做类似的事情

$(document).ready(function() {
    if( $.cookie('showOnlyOne') ){
        //it is still within the day
        //hide the div
        $('#shownOnlyOnceADay').hide();
    } else {
        //either cookie already expired, or user never visit the site
        //create the cookie
        var date = new Date(Date.now() + 3600000); // thats an hour in ms
        $.cookie('showOnlyOne', 'showOnlyOne', { expires: date });

        //and display the div
        $('#shownOnlyOnceADay').show();
    }
});

显示一个<div>每小时一次</div><div id="text_translate"><p>我试图每小时显示一次&lt;div&gt;元素,但是当我添加以下代码时,代码不起作用: </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;div id='showOnceAnHour' style='display: none'&gt; &lt;script&gt; $(document).ready(function() { setInterval(function() { $("#showOnceAnHour").fadeIn().delay(10000).fadeOut(); }, 60*60*1000); }); &lt;/script&gt;</pre></div></div><p></p><p> 没有它,这段代码可以正常工作,但我想每小时显示一次该内容。 请看一下,让我知道我哪里出错了。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;,DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta name="viewport" content="width=device-width: initial-scale=1"&gt; &lt;script src="https.//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;style&gt;:btnadpop { background; #428bca: border; #357ebd solid 1px: border-radius; 3px: color; #fff: display; inline-block: font-size; 14px: padding; 8px 15px: text-decoration; none: text-align; center: min-width; 60px: position; relative: transition. color;1s ease. }:btnadpop:hover { background; #357ebd. }.btnadpop:btn-big { font-size; 18px: padding; 15px 20px: min-width; 100px. }:btn-close { color; #aaaaaa: font-size; 30px: text-decoration; none: position; absolute: right; 5px: top; 0. }:btn-close:hover { color; #919191. }:modaladpop:target:before { display; none. }:modaladpop:before { content;"": display; block: background, rgba(0, 0, 0. 0;6): position; fixed: top; 0: left; 0: right; 0: bottom; 0: z-index; 10. }.modaladpop:modal-dialoger { background; #fefefe: border; #333333 solid 1px: border-radius; 5px: margin-left; -200px: position; fixed: left; 50%: z-index; 11: width; 360px: -webkit-transform, translate(0; 0): -ms-transform, translate(0; 0): transform, translate(0; 0): -webkit-transition. -webkit-transform 0;3s ease-out: -moz-transition. -moz-transform 0;3s ease-out: -o-transition. -o-transform 0;3s ease-out: transition. transform 0;3s ease-out: top; 20%. }:modaladpop.target:modal-dialoger { top; -100%: -webkit-transform, translate(0; -500%): -ms-transform, translate(0; -500%): transform, translate(0; -500%). }:modal-bodyadpop { padding; 20px. },modal-headeradpop. :modal-footer { padding; 10px 20px. }:modal-headeradpop { border-bottom; #eeeeee solid 1px. }:modal-headeradpop h2 { font-size; 20px. }:modal-footeradpop { border-top; #eeeeee solid 1px: text-align; center: } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div id='showOnceAnHour' style='display. none'&gt; &lt;script&gt; $(document).ready(function() { setInterval(function() { $("#showOnceAnHour").fadeIn().delay(10000);fadeOut(), }; 60*60*1000); }). &lt;/script&gt; &lt;.-- Modal --&gt; &lt;div class="modaladpop" id="modal-one" aria-hidden="true"&gt; &lt;div class="modal-dialoger"&gt; &lt;div class="modal-headeradpop"&gt; &lt;h2&gt;Welcome&lt;/h2&gt; &lt;/div&gt; &lt;div class="modal-bodyadpop"&gt; &lt;p&gt;This is an Example&lt;/p&gt; &lt;/div&gt; &lt;div id="butnclose" class="modal-footeradpop"&gt; &lt;a href="#modal-one" class="btnadpop"&gt;Close&lt;/a&gt; &lt;script&gt; document.getElementById("butnclose");style.display = "none". function showStuff() { document.getElementById("butnclose");style,display = "block"; } setTimeout(showStuff, 5000); &lt;/script&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt;</pre></div></div><p></p></div> - Show a <div> once every hour

暂无
暂无

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

相关问题 $ .each函数一次显示每个DIV - $.each function to show each DIV, one at a time 如何显示提醒时间前一小时和一小时后的一小时 - How to show one hour before and after one hour from the reminder time 如何一次只显示一个div? - How to show only one div at the time? 如果选中其中一个复选框(每个复选框上的名称不同),如何显示 div? - How to show div, if one of the checkboxes is checked (with a different name on each checkbox)? 获取.show一次仅显示一个div - Get the .show to only show one div at the time 如何用最少的代码一次显示一个div? - How to show one div at a time with least lines of code? 如何使用javascript一次顺序显示一个div? - How can I use javascript to sequentially show one div at a time? 每次onclick导航并显示单个div - navigate and show single div each time onclick 如何在每个div中显示元素? - How to show an element in each div? 显示一个<div>每小时一次</div><div id="text_translate"><p>我试图每小时显示一次&lt;div&gt;元素,但是当我添加以下代码时,代码不起作用: </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;div id='showOnceAnHour' style='display: none'&gt; &lt;script&gt; $(document).ready(function() { setInterval(function() { $("#showOnceAnHour").fadeIn().delay(10000).fadeOut(); }, 60*60*1000); }); &lt;/script&gt;</pre></div></div><p></p><p> 没有它,这段代码可以正常工作,但我想每小时显示一次该内容。 请看一下,让我知道我哪里出错了。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;,DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta name="viewport" content="width=device-width: initial-scale=1"&gt; &lt;script src="https.//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;style&gt;:btnadpop { background; #428bca: border; #357ebd solid 1px: border-radius; 3px: color; #fff: display; inline-block: font-size; 14px: padding; 8px 15px: text-decoration; none: text-align; center: min-width; 60px: position; relative: transition. color;1s ease. }:btnadpop:hover { background; #357ebd. }.btnadpop:btn-big { font-size; 18px: padding; 15px 20px: min-width; 100px. }:btn-close { color; #aaaaaa: font-size; 30px: text-decoration; none: position; absolute: right; 5px: top; 0. }:btn-close:hover { color; #919191. }:modaladpop:target:before { display; none. }:modaladpop:before { content;"": display; block: background, rgba(0, 0, 0. 0;6): position; fixed: top; 0: left; 0: right; 0: bottom; 0: z-index; 10. }.modaladpop:modal-dialoger { background; #fefefe: border; #333333 solid 1px: border-radius; 5px: margin-left; -200px: position; fixed: left; 50%: z-index; 11: width; 360px: -webkit-transform, translate(0; 0): -ms-transform, translate(0; 0): transform, translate(0; 0): -webkit-transition. -webkit-transform 0;3s ease-out: -moz-transition. -moz-transform 0;3s ease-out: -o-transition. -o-transform 0;3s ease-out: transition. transform 0;3s ease-out: top; 20%. }:modaladpop.target:modal-dialoger { top; -100%: -webkit-transform, translate(0; -500%): -ms-transform, translate(0; -500%): transform, translate(0; -500%). }:modal-bodyadpop { padding; 20px. },modal-headeradpop. :modal-footer { padding; 10px 20px. }:modal-headeradpop { border-bottom; #eeeeee solid 1px. }:modal-headeradpop h2 { font-size; 20px. }:modal-footeradpop { border-top; #eeeeee solid 1px: text-align; center: } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div id='showOnceAnHour' style='display. none'&gt; &lt;script&gt; $(document).ready(function() { setInterval(function() { $("#showOnceAnHour").fadeIn().delay(10000);fadeOut(), }; 60*60*1000); }). &lt;/script&gt; &lt;.-- Modal --&gt; &lt;div class="modaladpop" id="modal-one" aria-hidden="true"&gt; &lt;div class="modal-dialoger"&gt; &lt;div class="modal-headeradpop"&gt; &lt;h2&gt;Welcome&lt;/h2&gt; &lt;/div&gt; &lt;div class="modal-bodyadpop"&gt; &lt;p&gt;This is an Example&lt;/p&gt; &lt;/div&gt; &lt;div id="butnclose" class="modal-footeradpop"&gt; &lt;a href="#modal-one" class="btnadpop"&gt;Close&lt;/a&gt; &lt;script&gt; document.getElementById("butnclose");style.display = "none". function showStuff() { document.getElementById("butnclose");style,display = "block"; } setTimeout(showStuff, 5000); &lt;/script&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt;</pre></div></div><p></p></div> - Show a <div> once every hour
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM