Coffeescript 等效于==
is is
,实际上等效于===
(检查值和类型)。
话虽这么说,if scrollComp.isMoving == false
说起来有点尴尬,在 JS中说类似unless scrollComp.isMoving
或这样的东西更有意义。if(!scrollComp.isMoving)
好的,对于您的问题的解决方案(我不相信上述两件事中的任何一个都可以真正解决),当您执行这些print
语句时,您很可能是在脚本启动时这样做而不是在事件处理程序中的异步。当您的页面加载时,是您的代码进入该 if/else 语句时,此时您不会滚动,因此它将始终是false
. 要捕捉滚动的瞬间并在滚动发生时运行代码,您需要注册一个事件监听器:
scrollComp.onMove ->
// Scrolling here! Do fancy stuff!
print scrollComp.isMoving // 'true'
现在,为了能够在滚动停止 30 秒后触发函数调用,我们必须跟踪时间:
// Define interval as 30 seconds.
// (expressed in milliseconds)
interval = 30*1000
time = Date.now() // set timer to now
scrollComp.onMove ->
// We update the timer every time
// scroller moves.
time = Date.now()
// We need to create an infinite loop
// that will check the time since last
// move of the scroller, and execute
// a function when the time has surpassed
// some threshold.
setInterval ->
if (Date.now() - time) > interval
// It has been 30 seconds since
// scroller last moved.
, 5000
最后一个5000
数字就是运行时间检查的频率;这将每 5000 毫秒运行一次。
如果您真的想计算帧数,您可以通过计算帧速率并使用一些代数柔术来动态生成该interval
变量。