javascript确认阻止Chrome中的屏幕更新 [英] javascript confirm prevents screen update in Chrome

查看:56
本文介绍了javascript确认阻止Chrome中的屏幕更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个执行以下两项操作的javascript函数:

I have a javascript function that does two things:

  • 更改表格行的背景颜色
  • 发出一个Confirm()弹出窗口,要求用户确认(突出显示)行的删除

它在Firefox上运行良好.在Chrome上,出现弹出窗口.但是,直到我关闭了confirm()框之后,背景才会改变颜色,这有点违背了让用户知道要删除哪一行的目的.

It works fine on Firefox. On Chrome, the popup appears. But the background does not change color until AFTER I dismiss the confirm() box which kinda defeats the objective of letting the user know what row is to be deleted.

我确定它与javascript的异步特性有关.但是我需要知道如何解决它.现在,两行代码是:

I'm sure it has to do with the asynchronous nature of javascript. But I need to know how to get around it. Right now, the two lines of code are:

  $(icon).closest( "tr" ).css( "background-color", "yellow" );
  if ( confirm( message )) {.......}

我需要做些什么来确保显示弹出窗口时该行是黄色的,并且等到弹出窗口消失后才等到变为黄色?我可以尝试延迟,等等.但这很困难.是否有正确"的方式来处理此问题?

What do I need to do to make sure the row is yellow while the popup is displayed and doesn't wait to change to yellow until after the popup goes away? I can try delays, etc. But that's grasping at straws. Is there a 'correct' way to handle this?

再次,可以在Firefox上正常工作....在Chrome上为nada.尚未尝试其他浏览器.

Again, works fine on Firefox.... nada on Chrome. Haven't tried other browsers.

谢谢.杰瑞

推荐答案

我可以尝试延迟,等等.但这很困难.

I can try delays, etc. But that's grasping at straws.

不是真的.

渲染内容完全取决于浏览器.每个浏览器引擎都有其自己的优化.解决此问题的唯一方法是短暂的延迟.

It's entirely up to the browser when to render things. Each browser engine has its own optimizations. The only way to handle this is a short delay.

一种可靠的延迟形式是 requestAnimationFrame() .我认为您可以确定,一旦启动,浏览器将重新绘制以前的所有内容.未经测试,但尝试这样的事情:

One reliable form of delay is requestAnimationFrame(). I think you can be reasonably sure that once this is fired, the browser will have repainted anything previously. Untested, but try something like this:

$(icon).closest('tr').css('background-color', 'yellow');
requestAnimationFrame(function () {
  if (confirm(message)) {

  }
});

还请注意,您对确认框的显示位置没有任何控制.它可能在您的内容之上.由浏览器决定如何呈现,无论是传统的工具样式窗口还是全屏模式.

Also note that you don't have any control over where that confirm box appears. It could be on top of your content. It's up to the browser to decide how to present that, whether it be a traditional tool-style window, or a full-screen modal.

这篇关于javascript确认阻止Chrome中的屏幕更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆