在我继续之前,请允许我说,因为设计师的逻辑难以实现,所以我感谢你承认我的无知。
我正在创建一个简单的滑块,我希望这样当用户用鼠标移动图片时,程序可以记下mouseup()之前的最后一秒所覆盖的距离,这样我就可以了计算图片必须移动到模拟的程度,就像它被刷过一样。
我的基本事件处理程序如下:
$('.testdiv').mousedown(function(){
pretestFunction();
//mouse is pressed
mousepressed = 1;
//set first X point
prevX = event.pageX;
}).mouseup(function(){
//get stop X point
stopX = event.pageX;
mousepressed = 0;
});
pretestFunction调用setInterval,如下所示:
function pretestFunction(){
window.setInterval(testFunction(), 1000);
}
testfunction():
function testFunction(){
console.log('1 second');
}
我的问题是设置间隔功能只调用一次,而不是每秒调用一次,就像我试图让它一样。我做错了什么?
答案 0 :(得分:5)
window.setInterval(testFunction(), 1000);
应该是
window.setInterval(testFunction, 1000);
首先,您传递的值正在调用 testFunction
(恰好是undefined
,但这并不重要)
在第二个中你将传递 testFunction
的值(即一个函数,这就是你想要的。)