首页 > 解决方案 > 如何在我的网页上每 15 秒打开一次神奇的弹出窗口

问题描述

我在我的网页上使用 JavaScript 代码,但它在 10 秒后仅出现 2 次,我想每 15 秒显示一次弹出窗口,比如无限次,请帮帮我,我无法找到解决方案,这里是 JavaScript 代码:

$(window).load(function() {
    setTimeout(function() {
        $.magnificPopup.open({
            items: {
                src: 'Contact_Form/index.html'
            },
            type: 'iframe',
            mainClass: 'my-mfp-zoom-in'
        });
    }, 10000);
});

还有一件事,如果有人点击我的网页,就会出现这个弹出窗口,我们该怎么做,那里的任何人都解决了我的问题,我不知道如何解决它。

标签: javascripthtmljquerycss

解决方案


使用setInterval()而不是setTimeout. 这将在指定的时间段后无限重复。

function alertUser() {
  alert("Hello");
}

//This will run on page load
alertUser();

//This will wait 1000ms/1s first and then run the function and will repeat infinitely
setInterval(alertUser(), 1000);

编辑:

要在评论中回答您的问题,只需使用:

window.addEventLister("onclick", alertUser());

推荐阅读