Javascript 为什么触发touchstart事件时setTimeout游戏循环会出现延迟?

Javascript 为什么触发touchstart事件时setTimeout游戏循环会出现延迟?,javascript,android,google-chrome,debugging,game-loop,Javascript,Android,Google Chrome,Debugging,Game Loop,这是我遇到的一个相当具体的问题。该故障似乎只出现在最新的Android版Chrome更新中,因此以下是规格: 应用程序版本: 铬45.0.2454.84 操作系统: 安卓4.4.4;XT1031构建/KXB21.14-L2.4 我使用的是Moto G,但故障也发生在运行同一版本Chrome的三星手机上。无论如何,问题显然是touchstart事件中断了我的窗口。setTimeout游戏循环。代码如下: ( function rafTouchGlitch() { /////

这是我遇到的一个相当具体的问题。该故障似乎只出现在最新的Android版Chrome更新中,因此以下是规格:

应用程序版本: 铬45.0.2454.84

操作系统: 安卓4.4.4;XT1031构建/KXB21.14-L2.4

我使用的是Moto G,但故障也发生在运行同一版本Chrome的三星手机上。无论如何,问题显然是
touchstart
事件中断了我的
窗口。setTimeout
游戏循环。代码如下:

(
    function rafTouchGlitch() {
        /////////////////
        /* FUNCTIONS. */
        ///////////////

        function touchStartWindow(event_) {
            event_.preventDefault();
        }

        ///////////////////////
        /* OBJECT LITERALS. */
        /////////////////////

        var engine = {
            /* FUNCTIONS. */
            start : function(interval_) {
                var handle = this;

                (function update() {
                    handle.timeout = window.setTimeout(update, interval_);

                    /* Draw the background and the red square. */
                    display.fillStyle = "#303030";
                    display.fillRect(0, 0, display.canvas.width, display.canvas.height);

                    display.fillStyle = "#f00000";
                    display.fillRect(red_square.x, red_square.y, 20, 20);

                    /* Update the square's position. */
                    red_square.x++;

                    if (red_square.x > display.canvas.width) {
                        red_square.x = -20;
                    }
                })();
            },
            /* VARIABLES. */
            timeout : undefined
        };

        red_square = {
            /* VARIABLES. */
            x : 0,
            y : 0
        };

        /////////////////
        /* VARIABLES. */
        ///////////////

        var display = document.getElementById("canvas").getContext("2d");

        //////////////////
        /* INITIALIZE. */
        ////////////////

        window.addEventListener("touchstart", touchStartWindow);

        engine.start(1000 / 60);
    })();
此代码在任何其他浏览器中都能正常工作,甚至在以前的Chrome版本中也能正常工作。它在上一版本中运行良好,但由于某种原因,现在出现了一个问题。我不确定这是否是Chrome端的一个bug,或者他们是否正在做一些新的事情,而我是一个没有涵盖我的基础的人,但事实仍然是,当
touchstart
事件触发时,我的游戏循环中有一个明显的滞后

这是一把小提琴:

点击屏幕,看看红方块是如何落后的!这太可笑了。通过足够快地点击屏幕,基本上可以冻结超时对象

如果你有Android手机,我建议你更新Chrome并访问链接,看看我的意思。如果这是一个bug,那么考虑到
setTimeout
的价值以及
touchstart
在移动设备上的重要性,这是一个巨大的bug。如果不是bug,我真的很想知道我做错了什么

谢谢你的时间

在touchstart func的末尾执行“返回错误;”

对于不带jquerymobile的刷卡,请使用以下jquery

$("#id").on({'touchstart' : functionforstart, 'touchmove' :onmove, 'touchend' : functionforend});

functionforstart(e){ e.originalEvent.touches[0].pageX usw.. return false;}

function onmove(e){lastMove=e}

function functionforend(){lastMove.originalEvent.touches[0].pageX} usw..    e.prefentDefault(); }
这对我很有用。。var lastMove必须是全局的,您需要一个touchmove步骤来获得touchend中的位置。。希望这也是你需要的

在touchstart func的末尾执行“返回错误;”

对于不带jquerymobile的刷卡,请使用以下jquery

$("#id").on({'touchstart' : functionforstart, 'touchmove' :onmove, 'touchend' : functionforend});

functionforstart(e){ e.originalEvent.touches[0].pageX usw.. return false;}

function onmove(e){lastMove=e}

function functionforend(){lastMove.originalEvent.touches[0].pageX} usw..    e.prefentDefault(); }
这对我很有用。。var lastMove必须是全局的,您需要一个touchmove步骤来获得touchend中的位置。。希望这也是你需要的

Android Chrome在touchmove期间不运行计划(setTimeout/setInterval)回调

作为一种解决方法,您可以尝试window.requestAnimationFrame()或在touchmove事件中手动运行回调,这将需要保留/检查上次运行时间。

请参阅

Android Chrome在touchmove期间不运行计划(setTimeout/setInterval)回调


作为一种解决方法,您可以尝试window.requestAnimationFrame()或在touchmove事件中手动运行回调,这将需要保留/检查上次运行时间。

非常感谢,您的答案值得更多的投票。谷歌搜索“touchstart lag”会返回一系列关于不同问题的结果,这些问题可以通过FastClick解决,也可以通过使用较旧的Chrome/Safari进行某种元标记用户可伸缩的修复。这个问题是唯一一个与这个具体问题相关的问题。真正的解决方法是使用requestAnimationFrame(),而不是setTimeout()。仍然很烦人,例如在jquery-3.3.1.js第3640行调用window.setTimeout(process);当您使用延迟方法/承诺时,是否会延迟导致明显的“缓慢应用程序”…对我来说,它只是在touchmove事件上使用
preventDefault
时没有执行setInterval。。只有在touchstart启动后才设置touchmove活动,非常感谢,你的答案应该得到更多的投票。谷歌搜索“touchstart lag”会返回一系列关于不同问题的结果,这些问题可以通过FastClick解决,也可以通过使用较旧的Chrome/Safari进行某种元标记用户可伸缩的修复。这个问题是唯一一个与这个具体问题相关的问题。真正的解决方法是使用requestAnimationFrame(),而不是setTimeout()。仍然很烦人,例如在jquery-3.3.1.js第3640行调用window.setTimeout(process);当您使用延迟方法/承诺时,是否会延迟导致明显的“缓慢应用程序”…对我来说,它只是在touchmove事件上使用
preventDefault
时没有执行setInterval。。只有在touchstart启动后才设置touchmove事件