我想使用一个简单的倒数计时器,从运行函数30秒开始,到0结束.没有毫秒.怎么编码?
bigloop=setInterval(function () {
var checked = $('#status_table tr [id^="monitor_"]:checked');
if (checked.index()===-1 ||checked.length===0 || ){
bigloop=clearInterval(bigloop);
$('#monitor').button('enable');
}else{
(function loop(i) {
//monitor element at index i
monitoring($(checked[i]).parents('tr'));
//delay of 3 seconds
setTimeout(function () {
//when incremented i is less than the number of rows, call loop for next index
if (++i < checked.length) loop(i);
}, 3000);
}(0)); //start with 0
}
}, index*3000); //loop period
Run Code Online (Sandbox Code Playgroud)
我有上面的代码,有时它工作,有时它不是.我想知道clearInterval是否真的清除了计时器?因为有这个monitor按钮只有在它monitoring起作用时才会被禁用.clearInterval当.outputRemove点击一个被调用的元素时,我有另一个.请参阅以下代码:
//remove row entry in the …Run Code Online (Sandbox Code Playgroud) myInterval = setInterval(function(){
MyFunction();
},50);
function MyFunction()
{
//Can I call clearInterval(myInterval); in here?
}
Run Code Online (Sandbox Code Playgroud)
间隔没有停止(没有被清除),如果我上面编码的是好的那么它将帮助我在别处查找造成问题的原因.谢谢.
编辑:假设它在调用clearInterval之前完成了几个间隔,这消除了对setTimeout的需要.
在处理Web应用程序的某些Javascript时,我注意到我曾使用过setTimeout,但我曾试图清除它,clearInterval并且它在Google Chrome和Internet Explorer 9中停止了超时.
是clearTimeout和clearInterval互换?
这是一个JSfiddle,有一个我正在谈论的例子.
我有一个函数,font-family每500毫秒更改一些文本使用setInterval(我只是练习JavaScript.)通过单击"开"按钮调用该函数,并且应该使用单独的按钮清除间隔,"关闭".但是,"关闭"按钮实际上并没有清除间隔,它只是继续.我怀疑这与范围有关,但我不确定如何以其他方式编写它.另外,我不想用jQuery做这个,因为毕竟我正在做这个学习.
<body>
<p><span id="go" class="georgia">go</span> Italian</p>
<p>
<button id="on" type="button" value="turn on">turn on</button>
<button id="off" type="button" value="turn off">turn off</button>
</p>
<script>
var text = document.getElementById("go");
var on = document.getElementById("on");
var off = document.getElementById("off");
var fontChange = function() {
switch(text.className) {
case "georgia":
text.className = "arial";
break;
case "arial":
text.className = "courierNew";
break;
case "courierNew":
text.className = "georgia";
break;
}
};
on.onclick = function() {
setInterval(fontChange, 500);
};
off.onclick = function() {
clearInterval(fontChange); …Run Code Online (Sandbox Code Playgroud) 假设有人(邪恶)为我们设置了一个计时器setInterval,但是我们不知道它的ID(我们没有对该对象的引用,setInterval正在返回,也没有它的值)
(function(){
setInterval(function(){console.log('pwned')},
10000)
})();
Run Code Online (Sandbox Code Playgroud)
有办法,如何清除它?是否有可能以其他方式访问计时器?或者至少特别是浏览器/ javascript引擎?
大卫·弗拉纳根(David Flanagan)接触了他的大型JSTDG.
setInterval() method, use in malicious code索引中的键指向
...某些浏览器会检测重复的对话框和长时间运行的脚本,并为用户提供停止它们的选项.但是恶意代码可以使用setInterval()等方法来加载CPU,也可以通过分配大量内存来攻击你的系统.Web浏览器没有一般的方法可以阻止这种火腿攻击.在实践中,这不是Web上的常见问题,因为没有人返回到从事此类脚本滥用的网站!
为了练习,我试图显示一个从0 - 9递增,然后从9 - 0递减,并无限重复的数字.
我迄今似乎代码接近,但在第二次迭代中setInterval的我2个各自函数的调用countUp,并countDown似乎彼此发生冲突,因为显示的数字没有在预期的顺序计数......然后浏览器崩溃.
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title>Algorithm Test</title>
</head>
<body onload = "onloadFunctions();">
<script type = "text/javascript">
function onloadFunctions()
{
countUp();
setInterval(countUp, 200);
}
var count = 0;
function countUp()
{
document.getElementById("here").innerHTML = count;
count++;
if(count == 10)
{
clearInterval(this);
countDown();
setInterval(countDown, 200);
}
}
function countDown()
{
document.getElementById("here").innerHTML = count;
count--;
if(count == 0)
{
clearInterval(this);
countUp();
setInterval(countUp, 200);
}
}
</script>
From 0 - 9, up and …Run Code Online (Sandbox Code Playgroud) 嗨,我想删除所有$interval在Angular中运行.在我的页面有很多$间隔和按钮点击我想删除所有间隔.我将如何做.
任何帮助表示赞赏.
我想在我的Angular 4应用程序中使用setInterval.
const inter = setInterval(() => {
// logic resulting in exitCondition
if(exitCondition) {
clearInterval(inter);
}
}, 1000);
Run Code Online (Sandbox Code Playgroud)
这个设置在vanilla javascript中运行良好,但clearInterval()似乎在Angular中不起作用.做了一些研究后,我找到了Angular 1.x的间隔服务:
https://docs.angularjs.org/api/ng/service/ $ interval
Angular 4有类似的东西吗?或者有一个解决方法使clearInterval()工作?
我是React的新手,我正在尝试创建一个带有启动和停止按钮的简单秒表.我正在撞墙,试图通过"停止"按钮上的onClick事件来清除InterInterval.我会为setInterval声明一个变量,然后使用clearInterval清除它.不幸的是它不起作用.有小费吗?先感谢您.
import React, { Component } from 'react';
class App extends Component {
constructor(props){
super(props);
this.state = {time:0}
this.startHandler = this.startHandler.bind(this);
}
getSeconds(time){
return `0${time%60}`.slice(-2);
}
getMinutes(time){
return Math.floor(time/60);
}
startHandler() {
setInterval(()=>{
this.setState({time:this.state.time + 1});
},1000)
}
stopHandler() {
//HOW TO CLEAR INTERVAL HERE????
}
render () {
return (
<div>
<h1>{this.getMinutes(this.state.time)}:{this.getSeconds(this.state.time)}</h1>
<button onClick = {this.startHandler}>START</button>
<button onClick = {this.stopHandler}>STOP</button>
<button>RESET</button>
</div>
);
}
}
export default App;
Run Code Online (Sandbox Code Playgroud) clearinterval ×10
setinterval ×8
javascript ×7
counter ×2
jquery ×2
timer ×2
angular ×1
angularjs ×1
animation ×1
reactjs ×1
scope ×1