JavaScript setInterval 和clearInterval 不起作用

发布于 2025-01-07 16:04:53 字数 800 浏览 0 评论 0原文

我试图在最简单的情况下使用 setInterval 和clearInterval:

var passiveInterval = "";
var activeInterval = "";

function myStartFunction()
{
    ...
    passiveInterval = window.setInterval(passiveCheck, pIntAmt);
    activeInterval = window.setInterval(activeCheck, aIntAmt);
    ...
}

function myEndFunction()
{
    ...
    if (passiveInterval != "")
    {
        alert("passiveInterval: " + passiveInterval);
        window.clearInterval(passiveItnerval);
        passiveInterval = "";
    }
    if (activeInterval != "")
    {
        window.clearInterval(activeInterval);
        activeInterval = "";
    }
    ...
}

令人难以置信的是警报触发,并给我正确的间隔值(整数),但随后clearInterval语句触发:

ReferenceError:Can't查找变量:passiveItnerval

我已经尝试过对窗口的每个排列进行此操作,并将其打在所有内容的前面,但没有任何效果......

I am trying to use setInterval and clearInterval in literally the simplest case possible:

var passiveInterval = "";
var activeInterval = "";

function myStartFunction()
{
    ...
    passiveInterval = window.setInterval(passiveCheck, pIntAmt);
    activeInterval = window.setInterval(activeCheck, aIntAmt);
    ...
}

function myEndFunction()
{
    ...
    if (passiveInterval != "")
    {
        alert("passiveInterval: " + passiveInterval);
        window.clearInterval(passiveItnerval);
        passiveInterval = "";
    }
    if (activeInterval != "")
    {
        window.clearInterval(activeInterval);
        activeInterval = "";
    }
    ...
}

The incredible thing is that the alert triggers, and gives me the correct value of the interval (an integer), but then the clearInterval statement triggers:

ReferenceError: Can't find variable: passiveItnerval

I have tried this with every permutation of window and this slapped on the front of everything, but nothing works...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沫雨熙 2025-01-14 16:04:53

错字。 crtl+f 表示“passiveItnerval”...“t”和“n”颠倒了。

另外,如果可能的话,请考虑使用 setTimeout 而不是 setIntervalsetInterval 可能会变得很麻烦。

Typo. crtl+f for "passiveItnerval"... the "t" and "n" are reversed.

Also, please consider using setTimeout instead of setInterval if possible. setInterval can get hairy.

2025-01-14 16:04:53

你有一个错字

PassiveItnerval != PassiveInterval

You've got a typo

passiveItnerval != passiveInterval

颜漓半夏 2025-01-14 16:04:53

我有一个简单的函数,你可以一遍又一遍地使用它:

var timer = function(t,i,d,f,fend,b) 
{
  if( !f )
   { return; }
  if( t == -1 || t > 0 )
   { setTimeout( function() { b=(f())?1:0; timer( (b)?0:(t>0)?--t:t, i+((d)?d:0), d, f, fend,b ); }, (b||i<0)?0.1:i ); }
  else if( typeof fend == 'function' )
        { setTimeout( fend, 1 ); }
};

parameters:
t = times/repeat count (-1 is endless)
i = interval wait period in milliseconds
d = Interval increase or decrease after each interval 
f = function to call after interval
fend = function to call when t ended

注意:当函数 f() 返回 true 时,即使循环无穷无尽,也可以停止循环,

使用起来非常简单:

timer(4,1000,0,function(){alert('called');},function(){alert('Thank you - finished');});

想象一下你可以用它做什么,它是无尽的;-)

问候和欢呼,
欧文·汉杰斯

I have a simple function for this and you can use it over and over again:

var timer = function(t,i,d,f,fend,b) 
{
  if( !f )
   { return; }
  if( t == -1 || t > 0 )
   { setTimeout( function() { b=(f())?1:0; timer( (b)?0:(t>0)?--t:t, i+((d)?d:0), d, f, fend,b ); }, (b||i<0)?0.1:i ); }
  else if( typeof fend == 'function' )
        { setTimeout( fend, 1 ); }
};

parameters:
t = times/repeat count (-1 is endless)
i = interval wait period in milliseconds
d = Interval increase or decrease after each interval 
f = function to call after interval
fend = function to call when t ended

NOTE: when function f() returns true loop can be stopped even when it is endless

very simple to use:

timer(4,1000,0,function(){alert('called');},function(){alert('Thank you - finished');});

Imagine what you can do with it, it is endless ;-)

Greetz and cheers,
Erwin Haantjes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文