[英]Countdown pause on scrolling on mobile devices but not on desktop
I am building a web app that is intended to run on mobile phones and desktop. 我正在构建一个旨在在手机和台式机上运行的Web应用程序。 A part of the app is a quiz.
该应用程序的一部分是测验。 I have a countdown clock at the top of the page that counts down one seconds at the time.
我在页面顶部有一个倒数时钟,当时的时间是倒数一秒。
window.setInterval(function () {
console.log("THIS STOPS WHILE SCROLLING");
}, 1000);
When the user touches the screen and holds on Android and iOS devices, this function pauses and then resumes when scrolling or touching is done/released. 当用户触摸屏幕并在Android和iOS设备上按住时,此功能会暂停,然后在滚动/触摸完成/释放时恢复。
I need the clock to keep updating and counting while the user touches the screen. 用户触摸屏幕时,我需要时钟来保持更新和计数。 How do I do that?
我怎么做? I am using
AngularJS
. 我正在使用
AngularJS
。
You can disable scrolling on touch devices using code like this: 您可以使用以下代码在触摸设备上禁用滚动:
var disableScroll = function (e) {
if (yourAlarmIsRunning === true) {
e.preventDefault();
}
};
document.body.addEventListener('touchstart', disableScroll, false);
document.body.addEventListener('touchmove', disableScroll, false);
Since it sounds like you only want to disable it if your alarm thing is running, use a variable to keep track of whether it's running, and if it is, then disable scrolling. 由于听起来好像您只想在警报事件正在运行时将其禁用,所以请使用变量来跟踪它是否正在运行,如果运行,则禁用滚动。
You can use some "scroll" library, to emulate the native scroll. 您可以使用一些“滚动”库来模拟本地滚动。 This way, the timeouts and intervals aren't paused ;)
这样,超时和间隔不会暂停;)
I had the same problem in the past, and this worked for me. 我过去也遇到过同样的问题,这对我有用。
I don't remember exactly wich library i used, but something like this may work: https://jamesflorentino.github.io/nanoScrollerJS/ 我不记得我曾经使用过哪一个库,但是这样的方法可能会起作用: https : //jamesflorentino.github.io/nanoScrollerJS/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.