Heim  >  Fragen und Antworten  >  Hauptteil

javascript - Schreiben Sie mit js ein Popup basierend auf einer festen Zeit, Popup dreimal

Verwenden Sie js, um eine Interpretation zu schreiben, zum Beispiel: Für eine Ebene erscheint sie beim ersten Mal nach 10 Sekunden, nach dem Schließen erscheint sie nach 20 Sekunden erneut und nach dem Schließen erneut nach 40 Sekunden Sekunden. Dann taucht es nicht auf.

var firstShow = 10000;
var secondShow = 20000;
var threeShow = 400000;

setTimeout(openMpM, firstShow);
function openMpM() {
    $("#swtCenter2").fadeIn(1000);
}

var clearAfter = setTimeout(openMpM, secondShow);
var clearAfter1 = setTimeout(openMpM, threeShow);

function closeM() {
    $("#swtCenter2").fadeOut(1000);
    setTimeout(openMpM, 50000);
}

Ich habe einfach keine Ahnung, wie ich ihn dazu bringen kann, nach drei Malen aufzuhören. Ich hoffe, ich kann es im Detail schreiben.

迷茫迷茫2686 Tage vor790

Antworte allen(4)Ich werde antworten

  • 迷茫

    迷茫2017-06-12 09:34:02

    最后一次弹出执行之后,把定时器清除不就行了…

    Antwort
    0
  • 代言

    代言2017-06-12 09:34:02

            function first(){
                alert('第一次');
                setTimeout(second,20000);
            }
            function second(){
                alert('第二次');
                setTimeout(third,40000);
            }
            function third(){
                alert('第三次')
            }
            setTimeout(first,10000);
    

    这样不知道能不能行

            var firstShow=1000;
            var secondShow=5000;
            var threeShow=10000;
            var n=0;
            setTimeout(openMpM,1000);
            function openMpM() {
                $("#swtCenter2").fadeIn(1000);
                $("#swtCenter2").fadeOut(1000);
                n++;
                switch (n){
                    case 1:
                    setTimeout(openMpM,secondShow);
                        break;
                    case 2:
                    setTimeout(openMpM,threeShow);
                        break;    
                }
            }

    显示隐藏条件 你自己加

    Antwort
    0
  • 迷茫

    迷茫2017-06-12 09:34:02

    <p id="box"></p>
        <button id="btn">hide</button>
        <script>
            var times = [2000, 4000, 6000];
            function State(times) {
                var self = this;
                this.times = times
                this.oBox = document.getElementById('box');
                this.btn = document.getElementById('btn');
                this.i = 0;
    
                this.btn.onclick = function() {
                    self.change()
                }
            }
            State.prototype.show = function() {
                var self = this;
                setTimeout(function() {
                    self.oBox.style.display = "block";
                }, this.times[this.i++])
            }
            State.prototype.change = function(time) {
                if(this.i == this.times.length) {
                    alert('没有数据了');
                    return;
                }
                this.oBox.style.display = 'none';
                this.time = time
                this.show();
            }
            var s = new State(times);
            s.show();
        </script>

    你参考下这个,关闭的时候是手动点击关闭的

    Antwort
    0
  • PHP中文网

    PHP中文网2017-06-12 09:34:02

    简单地实现了一下,不知道有没有满足你的需求

    var timer,num=4;
    $('关闭').on('click',function(){
      $("#swtCenter2").fadeOut();
      clearInterval(timer);
      alertTime();
    });
    
    function alertTime(){
      num--;
      if(num<=0){
       clearInterval(timer);
       return false;
      }
      timer=setInterval(function(){
        $("#swtCenter2").fadeIn();
      },10000);
    }
    alertTime();

    搞错时间了,稍微改了一下,应该就是这样

    var timer,num=-1;
    $('关闭').on('click',function(){
      $("#swtCenter2").fadeOut(0);
      clearInterval(timer);
      alertTime();
    });
    
    function alertTime(){
      num++;
      var idx=num;
      idx=!idx?0.5:idx;
      if(num>2){
       clearInterval(timer);
       return false;
      }
      timer=setInterval(function(){
        $("#swtCenter2").fadeIn(0);
      },10000*2*idx);
    }
    alertTime();

    Antwort
    0
  • StornierenAntwort