问题:当我在函数中声明clearInterval()/ setInterval()或clearTimeout()/ setTimeout()时,它什么都不做.我必须重置它的原因是用户可以单击以重置计时器.
我尝试了什么:我想要一个函数每3秒执行一次(因此我使用setInterval()与setTimeout()),并在点击时重置该计时器.我已经尝试使用setTimeout(),在每次执行函数时都使用clearTimeout()和setTimeout().结果:执行一次.
以下代码与setInterval相同.结果:它循环,永不重置.
// Declare variables as global
var varTimerSpeed = 3000;
var varTimerInterval;
varTimerInterval = setInterval("someFunction()", varTimerSpeed);
function someFunction() {
// Reset timer
clearInterval(varTimerInterval);
varTimerInterval = setInterval("someFunction()", varTimerSpeed);
}
// Call the function
$(".class").live("click",function() { someFunction(); });
Run Code Online (Sandbox Code Playgroud)
我在someFunction()中正在执行其他正确执行的事情,因此我知道click的处理程序正在运行.
可能重复:
setInterval()中的函数无延迟地执行
我试图每隔X几毫秒使用setTimeOut来调用calla函数,但它似乎只是吓坏了,无论我设置的时间范围如何都非常快?难道我做错了什么?
var Count = 0;
var GameRunning = 0;
var lblTimer = Titanium.UI.createLabel({
color:'#999',
text:'I am Window 1',
font:{fontSize:20,fontFamily:'Helvetica Neue'},
textAlign:'center',
width:'auto',
height: 25,
top: 25
});
var btnStartGame = Titanium.UI.createButton({
title: 'Start',
width: 50,
height: 25,
top: 75
});
function RunGame () {
Count++;
lblTimer.text = Count;
x = setTimeout(RunGame(), 100000);
}
function StartGame () {
if(GameRunning==0)
{
GameRunning = 1;
RunGame();
}
else
{
// Stop the game.
GameRunning = 0;
Count = 0; …Run Code Online (Sandbox Code Playgroud) 此代码似乎不起作用.我正在尝试创建一个可用的聊天室动态列表,每10秒更新一次.我还希望用户能够设置peraminters来过滤将显示的房间.我正在使用此代码,并且由于某种原因它似乎不起作用.
<script type="text/javascript">
function setRooms()
{
console.log('ok');
if($('#only').is(':checked'))
{
var checked = 1;
}
else
{
var checked = 0;
}
if($('#open').is(':checked'))
{
var opened = 1;
}
else
{
var opened = 0;
}
$.post('backend/outputRooms', {
fLevel : $('#flevel').val(),
sLevel : $('#slevel').val(),
display : $('display').val(),
Checked : checked,
},
function(data)
{
$('#text').html(data);
});
}
$(document).ready(function(){
setInterval(setRooms(), 10000);
});
</script>
Run Code Online (Sandbox Code Playgroud)
我已经排除了后端页面的问题,因为console.log('ok')似乎不能正常工作.有帮助吗?
例如,假设我使用工厂创建一组对象:
function factory() {
e.x = 0;
e.y = 0;
return e;
}
Run Code Online (Sandbox Code Playgroud)
如何将这些对象传递给渲染函数?
例如:
function render() {
a[0].someMethod;
}
function init() {
for(i=0;i<10;i++) {
things[i] = factory();
}
setInterval(render(things),40);
}
Run Code Online (Sandbox Code Playgroud) 我有这个代码,它运行一次然后停止.我希望它每十秒运行一次,我知道setinterval会循环代码,但由于某些原因它不起作用?
<script>
$(document).ready(setInterval(function(){
$("#club_color1 a").animate({color: '#000000'}, 1000);
$("#club_color2 a").animate({color: '#000000'}, 1000);
$("#club_color1 a").animate({color: '#FFFFFF'}, 1000);
$("#club_color2 a").animate({color: '#FFFFFF'}, 1000);
}),10000);
</script>
Run Code Online (Sandbox Code Playgroud) 下面是两个类似使用setInterval函数的简单示例:
http://codepen.io/anon/pen/MwYOOJ
在第一个示例中,setInterval函数不起作用,在使用以下语法的第二个版本中,它工作正常.
/* This part does not work */
var i = 0;
function displayNumber() {
i++;
$('#result').html(i);
}
setInterval(displayNumber(), 500);
/* This part does work */
var j = 0;
function displayNumberOk() {
j++;
$('#result-ok').html(j);
}
setInterval(function() { displayNumberOk() }, 500);
Run Code Online (Sandbox Code Playgroud)
在规范中,我可以找到:
setInterval(function,milliseconds,param1,param2,...)
Run Code Online (Sandbox Code Playgroud)
为什么我不能直接使用函数名?
如何根据时钟时间每15分钟自动刷新一次页面?
例如:刷新9:00,9:15,9:30,9:45,10:00,10:15,等等..
我见过一个像我想要的类似:https://stackoverflow.com/a/1217945/551559但我不认为它能完成这项工作.
setInterval(function(){
// check clock time on every minute??
if ( clock_time === '9:15' ) {
}
},1000);
Run Code Online (Sandbox Code Playgroud)
有人可以给我一个解决方案或任何链接来看待?
我目前正在全天候定期运行一个函数.
setInterval( function(){ do_this(); } , 1000*60);
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不是我想要的.我希望这个功能只能在早上9点到18点之间定期运行.该功能不应在这些时间之外运行.如何在node.js中完成?是否有方便的模块或功能可供使用?
我写过这个函数,每个函数每15秒自动调用一次.
setInterval(
function () {
check_alive_status();
},
15000 // every 15 seconds
);
Run Code Online (Sandbox Code Playgroud)
现在我的要求是,我必须只在前5分钟运行此功能,并且不应该调用它.我怎样才能实现它?有什么方法吗?
我需要检查是否有任何后台进程,如任何ajax调用或该网页中发生的文件上传.如果没有任何内容正在运行并且页面处于空闲状态,则在15分钟后,我必须注销该用户.怎么检查呢?谢谢!
<button id="friends" class="btn btn-primary large3">Friends</button>
setInterval(changeFriendBtnSize, 1000);
function changeFriendBtnSize() {
let upDown = 'up';
if (document.getElementById('friends').getElementsByClassName('large3')) {
console.log('111');
document.getElementById('friends').classList.remove('large3');
document.getElementById('friends').classList.add('large4');
} else if (document.getElementById('friends').getElementsByClassName('large4')) {
console.log('222');
document.getElementById('friends').classList.remove('large4');
document.getElementById('friends').classList.add('large3');
}
}
Run Code Online (Sandbox Code Playgroud)
在我的控制台日志中,它每秒一遍又一遍地记录111但从不记录222.类large3被删除而large4被添加但是从来没有反过来,为什么呢?
setinterval ×10
javascript ×9
jquery ×4
settimeout ×2
game-engine ×1
html5 ×1
if-statement ×1
mobile ×1
node.js ×1
timer ×1
timer-jobs ×1
titanium ×1