[英]What is the scope of setTimeout() and clearTimeout() in JavaScript?
30 seconds after a page is loaded/reloaded, I display a pop-up if certain conditions are met.页面加载/重新加载 30 秒后,如果满足某些条件,我会显示一个弹出窗口。 This is the code I use to achieve that an it works correctly:
这是我用来实现它正常工作的代码:
jQuery(document).ready(function($) {
..........
if (localStorage.getItem("displaypopup") == "true") {
var ticker = setTimeout(function() {
$('#colorboxform').click();
}, 30000);
}
..........
}
I have a button that triggers that same pop-up.我有一个触发相同弹出窗口的按钮。 If someone clicks that button I want to cancel the
setTimeout()
operation that I configured in the code above because I do not want for the pop-up to be invoked again, redundantly, when the user already clicked the button to display the pop-up.如果有人单击该按钮,我想取消我在上面的代码中配置的
setTimeout()
操作,因为当用户已经单击按钮以显示弹出窗口时,我不希望再次调用弹出窗口-向上。 In an attempt to cancel the setTimeout()
operation, I used this:为了取消
setTimeout()
操作,我使用了这个:
jQuery(document).ready(function($) {
..........
$("#headercolorboxform").click(
function() {
clearTimeout(ticker);
}
);
..........
}
Unfortunately, it is not working.不幸的是,它不起作用。 I still get the pop-up 30 seconds after the page was loaded/reloaded, even after I click the button to trigger the pop-up.
在页面加载/重新加载后 30 秒,我仍然会收到弹出窗口,即使在我单击按钮触发弹出窗口之后也是如此。 Apparently,
clearTimeout(ticker)
is not doing what I intended.显然,
clearTimeout(ticker)
没有按照我的意图进行。 I suspect this is a scope issue.我怀疑这是 scope 问题。 The pieces of my code together are:
我的代码片段是:
jQuery(document).ready(function($) {
..........
if (localStorage.getItem("displaypopup") == "true") {
var ticker = setTimeout(function() {
$('#colorboxform').click();
}, 30000);
}
$("#headercolorboxform").click(
function() {
clearTimeout(ticker);
}
);
..........
}
Do you know what the problem is in my code?你知道我的代码有什么问题吗? Why
clearTimeout(ticker)
is not cancelling setTimeout()
.为什么
clearTimeout(ticker)
没有取消setTimeout()
。 Maybe this is a scope problem?也许这是 scope 问题? I appreciate your help.
我感谢您的帮助。 Thank you.
谢谢你。
UPDATE 1:更新 1:
I tried the following, assuming I am dealing with a scope problem, but it does not work.我尝试了以下方法,假设我正在处理 scope 问题,但它不起作用。 I still get the pop-up redundantly after 30 seconds.
30 秒后,我仍然会重复弹出窗口。
jQuery(document).ready(function($) {
..........
var ticker;
if (localStorage.getItem("displaypopup") == "true") {
ticker = setTimeout(function() {
$('#colorboxform').click();
}, 30000);
}
$("#headercolorboxform").click(
function() {
clearTimeout(ticker);
}
);
..........
}
I followed the example at https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_cleartimeout as the foundation to write my code.我按照https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_cleartimeout中的示例作为编写代码的基础。 This was my solution that worked correctly as I wanted:
这是我想要的正常工作的解决方案:
jQuery(document).ready(function($) {
..........
var ticker;
function startCounting30Seconds() {
ticker = setTimeout(function() {
$('#colorboxform').click();
}, 30000);
}
function cancelCounting30Seconds() {
clearTimeout(ticker);
}
if (localStorage.getItem("displaypopup") == "true") {
startCounting30Seconds();
}
$("#headercolorboxform").click(function(){
cancelCounting30Seconds();
}
);
..........
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.