Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

javascript - 停止JavaScript中的setInterval调用(Stop setInterval call in JavaScript)

I am using setInterval(fname, 10000);

(我正在使用setInterval(fname, 10000);)

to call a function every 10 seconds in JavaScript.

(在JavaScript中每10秒调用一次函数。)

Is it possible to stop calling it on some event?

(是否可以在某个事件中停止调用它?)

I want the user to be able to stop the repeated refresh of data.

(我希望用户能够停止重复刷新数据。)

  ask by cnu translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

setInterval() returns an interval ID, which you can pass to clearInterval() :

(setInterval()返回一个间隔ID,您可以将其传递给clearInterval() :)

var refreshIntervalId = setInterval(fname, 10000);

/* later */
clearInterval(refreshIntervalId);

See the docs for setInterval() and clearInterval() .

(有关setInterval()clearInterval()请参阅文档。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...