单击“+”时,Internet Explorer 会冻结执行拖动鼠标操作。如何检测出现这种情况的原因呢?

发布于 2024-12-10 10:22:32 字数 929 浏览 1 评论 0原文

我正在开发一个用于创建简单图表的 JavaScript 应用程序,但我在 Internet Explorer(版本 8)中遇到了一些性能问题。

该应用程序可用于在图表上画线(见图)。有两个主要的处理程序用于绘制一条线。 ONMOUSEUP 事件拾取用户的单击,这表明用户想要在线上放置一个点并开始一个新线段(线由多个线段组成)。

ONMOUSEMOVE 处理程序会绘制一个淡出的线段,作为用户绘制线条时的视觉帮助。

画线过程截图请参考以下图片: https://i.sstatic.net/AxcFr.jpg http://i51.tinypic.com/fxjqf.jpg

如果用户单击鼠标 (mousedown) 但没有释放它,而是开始拖动鼠标,IE 会完全冻结。一段时间后,它将解冻并照常进行。

另一方面,Chrome 可以很好地处理这个问题(当拖动操作发生时,MOUSEMOVE 处理程序将完成其工作,并且仅在 ONMOUSEUP 事件触发后才放置该点。

我的理论是,资源管理器在这种情况下可能是单线程的他拾取 MOUSEMOVE 事件,但仅在 ONMOUSEUP 事件完成后才执行它们(这意味着在拖动操作期间基本上没有执行任何操作),但我不知道如何检查这是否属实,或者 。

我已经使用 IE8 内置 JS 分析器进行了一些分析,并且两个处理程序都被调用了通常的次数,没有任何函数被调用数千次,这会导致分析器冻结 IE 被冻结并且不输出任何内容,直到解冻,之后结果看起来与没有冻结操作相同

编辑:这是根据 dynaTrace 分析器工具的时间线:http://i51.tinypic.com/348sxty.jpg。

I am working on a JavaScript application for creating simple diagrams, and I have some performance issues in Internet Explorer (version 8).

The app can be used to draw lines on a diagram (See picture). There are two main handlers for drawing a line. An ONMOUSEUP event picks up user's clicks which indicate that the user wants to place a point on the line and start a new segment (lines consist of multiple segments).

An ONMOUSEMOVE handler draws a faded-out segment as a visual help to the user while he is drawing the line.

Please refer to these images for a screenshot of line-drawing process:
https://i.sstatic.net/AxcFr.jpg
http://i51.tinypic.com/fxjqf.jpg

If a user clicks the mouse (mousedown) but doesnt release it, and instead starts dragging the mouse around, IE completely freezes. After some time, it will unfreeze and proceed as usual.

Chrome on the other hand, handles this perfectly well (while the drag operation is happening, the MOUSEMOVE handler works its job, and the point is placed only after the ONMOUSEUP event fires.

My theory is that Explorer might be single-threaded in which case he picks up the MOUSEMOVE events but performs them only after the ONMOUSEUP event has completed (which means that esentially nothing is done for the duration of the drag operation), but I don't know how to check if this is true, or how to fix it.

I have done some profiling with the IE8 built-in JS profiler, and both handlers are called the usual amount of times, there are no functions being called thousands of times which would cause the freeze. The profiler is frozen while IE is frozen and outputs nothing until it unfreezes, after which the results seem the same as if there were no freeze action.

EDIT: Here is the timeline according to the dynaTrace profiler tool: http://i51.tinypic.com/348sxty.jpg.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰◇生如夏花灿烂 2024-12-17 10:22:32

问题是更新 DOM 是一个非常昂贵的操作。现在,如果没有限制,mousemove 事件会不断触发,如果您在每次触发的 mousemove 事件上更新 DOM,旧版浏览器就无法跟上。

解决方案可以将 mousemove 事件限制至少 200 毫秒。这意味着您只会在鼠标停止移动 200 毫秒后更新 DOM,从而给浏览器一些恢复时间。

这是如何限制 mousemove 事件的:

jQuery( "#element").bind( "mousemove", function(){
//Code to react to mousemove goes here
//Also add code here to check if the position has actually changed from last time,
//So you don't update the DOM for nothing
}.throttle( 200 ) );

限制方法所需的代码:

Function.prototype.throttle = function( delay ) {
var timeridto = 0, callback = this;

    function r(){
    var     args = Array.prototype.slice.call( arguments ),
        self = this;
        clearTimeout( timeridto );
        timeridto = setTimeout( function(){callback.apply( self, args ); }, delay );
    };

    r.cancel = function(){
     clearTimeout( timeridto );   
    }

    return r;
};

如果这不能解决问题,那么您可以使用 html5 canvas 或 (excanvas for IE) 用于绘图操作。

The problem is that updating the DOM is a very expensive operation. Now, the mousemove event fires constantly if not throttled and older browsers just can't keep up if you are updating the DOM on every mousemove event that is fired.

A solution could be throttling the mousemove event by at least 200 milliseconds. This means that you will only update the DOM after the mouse has stopped moving for 200 milliseconds, giving the browser some time to recover.

This is how you would throttle the mousemove event:

jQuery( "#element").bind( "mousemove", function(){
//Code to react to mousemove goes here
//Also add code here to check if the position has actually changed from last time,
//So you don't update the DOM for nothing
}.throttle( 200 ) );

Code required for the throttle method:

Function.prototype.throttle = function( delay ) {
var timeridto = 0, callback = this;

    function r(){
    var     args = Array.prototype.slice.call( arguments ),
        self = this;
        clearTimeout( timeridto );
        timeridto = setTimeout( function(){callback.apply( self, args ); }, delay );
    };

    r.cancel = function(){
     clearTimeout( timeridto );   
    }

    return r;
};

If this doesn't solve the problem then you could use the html5 canvas or (excanvas for IE) for drawing operations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文