由于我需要将匿名函数传递给setInterval我是否需要参数,因此我尝试使用以下代码。最初我让它调用this.countUp,但是当它返回时,NaN我做了一些阅读并.call(this)在 SO 上找到了解决方案。然而,当我将它与匿名函数(我承认我有点迷茫)结合起来时,我现在得到了TypeError: this.countUp is undefined了 Firebug。
我想我不需要使count可访问性,也不需要playBeep方法,但让我们假装我想要这样我就可以理解我在这段代码中做错了什么。
function workout() {
var beep = new Audio("beep1.wav");
this.timerWorkout; //three timers in object scope so I can clear later from a different method
this.timerCounter;
this.timerCoolDown;
this.count = 0;
this.startWorkout = function() {
alert(this.count);
this.timerWorkout = setTimeout(this.playBeep, 30 * 1000); //workout beep - 30 seconds
this.timerCounter = setInterval(function() {this.countUp.call(this)}, 1000); //on screen timer - every second
}
this.startCoolDown = …Run Code Online (Sandbox Code Playgroud) 我读StackOverflow上的答案在这里解释了如何使用的setTimeout来创造一种在网页上反复循环。但是,我想知道为什么 setTimeout 需要匿名函数才能工作。考虑以下代码:
trainingTimer(60,0);
function trainingTimer(duration,c) {
document.getElementById("timer").innerHTML = "Time left: "+(duration-c);
if(duration==c) {
alert("Time is up!");
}
else {
window.setTimeout(trainingTimer(duration,c+1),1000);
}
}
Run Code Online (Sandbox Code Playgroud)
我是根据上面链接的答案写的,但是当它运行时,它会trainingTimer一次遍历所有 60 个调用,并立即在我的网页上显示“剩余时间:0”。但是,如果我修改
window.setTimeout(trainingTimer(duration,c+1),1000);
并trainingTimer(duration,c+1)像这样包装一个匿名函数
window.setTimeout(function() {trainingTimer(duration,c+1)},1000);
那么代码工作得很好。
我想知道为什么我必须将命名函数包装在匿名函数中才能使 setInterval 正常工作。感谢所有的帮助。
我想把处理程序传递setInterval()给内部函数.
例如:
var id = setInterval(myfunction, "100000");
Run Code Online (Sandbox Code Playgroud)
我希望id转到myfunction
我试过了
setInterval(myfunciton(e), "10000");
Run Code Online (Sandbox Code Playgroud)
但它没有通过变量.
有任何想法吗?
很简单,setIntervall 通常以 't' 开头,go 为 false,clearinterval 什么也不做,go 为 true。所以如果 app.post 第三次执行,我有 2 个间隔,处理 t。我不明白为什么。
go= true;
app.post('/s', function s(req, res){
if (go){
setInterval(t, 3000);
go = false;
}else{
clearInterval(t);
go = true;
}
});
Run Code Online (Sandbox Code Playgroud) 我简化了它以更好地了解我的需要:我要做的任务是,我们有一个漫长的未知的nodejs进程(有很多排队的异步函数,我们不知道它们何时完成或类似的工作),并且想要有一个更新过程,我们将当前过程状态存储在数据库中。为此,我们有一个启动(它存储“开始”),一个结束过程位于之上process.on('beforeExit', function () { ... });。现在,我们必须处理客户要求的“仍在运行”过程。为此,我们想每十分钟更新一次带有时间戳的运行状态(此函数已经存在,并且称为state.setRunningState()
现在我有一个问题,我如何每十分钟触发一次该功能。为此,我打算在工作过程的每个事件上触发此函数,并比较它是否早于10分钟。问题是:有时会有更多的时间没有任何事件。所以第二个选择是setInterval(),这就是我的问题:如果我使用setInterval,我的nodejs进程将永远不会结束,因此该进程将无限运行直到清除Interval。但我也不知道该何时打电话clearInterval()
所以问题是:有没有一种方法可以创建这样的超时而不延长nodejs进程的生存时间。如果一切都完成了,它应该结束并忽略其余时间间隔。
我有一个功能如下:
function foo(args1, args2, retry)
{
if (retry <= 0)
return false;
var isDone = callAnotherFunction(args1, args2);
if(!isDone) {
setInterval(function () {
foo(args1, args2, retry-1);
},
2000);
}
else
return true;
}
Run Code Online (Sandbox Code Playgroud)
所以我不确定上面的实现是否正确。但是我需要在另一个函数中使用这个函数。并在 if 块中使用上述函数来决定是否需要执行其他语句。下面是上述函数的用法。
function useIt(args1, args2)
{
// Other code
let store = function() {
if(!foo(args1, args2, 5)) {
cleanStorage(args1, args2);
return;
}
}
Run Code Online (Sandbox Code Playgroud)
所以问题出在函数上useIt(),如果我使用or ,cleanStorage()则不会等待foo()执行。那么我需要如何实现这个功能呢?请帮助我。setIntervalsetTimeOutfoo()
我需要每隔几分钟从服务器获取更新,因此我在 DeliveriesService 中使用 setInterval。
这是我的相关部分 deliveries.service.ts
import { Injectable, OnInit } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import { Http } from '@angular/http';
import { Delivery, Product } from './delivery';
@Injectable()
export class DeliveriesService implements OnInit {
private fetchUrl = 'https://my.url';
private getInt;
public deliveries$ = new Subject<Array<Delivery>>();
constructor(private http: Http) { }
ngOnInit() {
this.startUpdate();
}
startUpdate(): void {
console.log('starting delivery fetch');
this.getInt = setInterval(this.fetchDeliveries(), 5 * 60 * 1000);
}
stopUpdate(): void {
clearInterval(this.getInt);
}
updateNow(): …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个模拟时钟。
second_hand = document.getElementById('second_hand');
Run Code Online (Sandbox Code Playgroud)
我有一个function,得到时间。
function getTime() {
var date = new Date(),
second = date.getSeconds();
return second;
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个function旋转手。
function rotateHand(hand) {
var second = getTime();
hand.style.transformOrigin = "0 100%";
hand.style.transform = 'rotate(' + second * 6 + 'deg)';
}
Run Code Online (Sandbox Code Playgroud)
然后我使用 asetInterval来更新我的rotateHand()每一秒。
setInterval(rotateHand(second_hand), 1000);
Run Code Online (Sandbox Code Playgroud)
但是我的手不是每秒更新(移动)。怎么了?
这是一个版本:
second_hand = document.getElementById('second_hand');
Run Code Online (Sandbox Code Playgroud)
function getTime() {
var date = new Date(),
second = date.getSeconds();
return second;
}
Run Code Online (Sandbox Code Playgroud)
我的 componentDidMount 中有一个操作,它在 30000 毫秒后从 Reddit 获取图像。我想测试这个但无法弄清楚?!?!有什么建议?
componentDidMount() {
this.getHumorEvery(30000)
}
getHumorEvery = async (ms) => {
await this.props.getHumor()
return setInterval(() => {
this.props.getHumor()
}, ms)
Run Code Online (Sandbox Code Playgroud)
所以 getHumor() 立即被调用。然后在 30000 毫秒后再次调用它,依此类推。
然后我的测试...
it('should refetch images every 30000 ms', () => {
const clock = sinon.useFakeTimers();
const getHumorSpy = sinon.spy();
renderComponent(requiredProps({ getHumor: getHumorSpy }));
clock.tick(30000)
sinon.assert.calledTwice(getHumorSpy)
Run Code Online (Sandbox Code Playgroud)
})
测试失败,因为 getHumor 只被调用一次。setInterval() 永远不会被解雇?
我正在将Turbolinks 5与Rails 5一起使用,并且我正在设置一个 JS 间隔,以便通过 AJAX 每 10 秒刷新一次页面内容:
<script type="text/javascript">
$(document).ready(function() {
function fetchContent() {
// some AJAX request
}
setInterval(fetchContent, 10000);
});
</script>
Run Code Online (Sandbox Code Playgroud)
当我第一次加载包含上述代码的页面时,它可以工作:fetchContent每 10 秒触发一次。
但是,当我导航到另一个页面时,它fetchContent一直在触发。此外,当我返回该页面(使用浏览器后退按钮)时,fetchContent会不可预测地多次触发。显然,这不是想要的行为。
如何解决这个问题?
注意:要解决这个问题,我知道我可能需要某种clearInterval调用,但是我应该如何使用 Turbolinks 5 进行调用,例如我应该绑定什么事件?从 Turbolinks 5 开始,API 发生了变化。特别是,所有 Turbolinks Classic 事件都已重命名,并且有几个(包括)page:update已被删除,因此我无法使用诸如this、this或this 之类的解决方案。
ruby-on-rails setinterval turbolinks ruby-on-rails-5 turbolinks-5
setinterval ×10
javascript ×8
node.js ×2
settimeout ×2
angular ×1
anonymous ×1
asynchronous ×1
function ×1
object ×1
promise ×1
reactjs ×1
sinon ×1
timer ×1
turbolinks ×1
turbolinks-5 ×1
typescript ×1