简体   繁体   English

Backbone.js-CSS更改正在等待JavaScript完成-在Firefox中有效,而在Chrome / Safari中不起作用

[英]Backbone.js - CSS change is waiting till javascript is completed - works in Firefox, not in Chrome/Safari

I am having a backbone.js application that I am writing. 我有一个正在编写的bone.js应用程序。

When user press a "Search" button, I show a loading.gif image (by making it block), while I let the javascript code to continue. 当用户按下“搜索”按钮时,我将显示一个loading.gif图像(将其阻止),同时让javascript代码继续。 Once the javascript code is complete, I unhide the loading image (changing the display to none). JavaScript代码完成后,我将取消隐藏正在加载的图片(将显示更改为无显示)。

I am able to see it working in Firefox. 我能够看到它在Firefox中工作。 In safari/and chrome, the change of CSS don't get applied until the javascript code is completed, and thus user don't see the loading image when the search is being performed. 在Safari和Chrome中,CSS的更改要等到javascript代码完成后才能应用,因此用户在执行搜索时看不到加载图片。

Any way to fix this? 有任何解决这个问题的方法吗?

Thanks 谢谢

A couple of things strike me as odd.. but to answer your question first: 有几件事使我感到奇怪。.但是首先要回答您的问题:

Most DOM/css changes do not get applied until the executing Javascript returns. 在执行的Javascript返回之前,大多数DOM / css更改都不会应用。 To get around this you can make your DOM change and then set a timeout to execute the rest of your Javascript code. 为了解决这个问题,您可以更改DOM,然后设置一个超时时间以执行其余的Javascript代码。

ex: 例如:

// make your image visible

function continuation() {
   // Put the javascript task that you need to execute here
}

// setTimeout will release execution control back to the browser so your CSS/DOM updates
// can be applied.  Once those updates are applied, continuation will be called
// by the browser and your remaining javascript can run.
setTimeout(continuation, 0); 

Now it seems odd that you would have any javascript that would take so long to run that you'd have time to even see a loading gif. 现在看来,您有任何需要花费很长时间才能运行的javascript,甚至都没有时间看到加载的gif,这似乎很奇怪。 It would make sense to see a loading image if your are firing an XHR (ajax) request but if you are doing that then you shouldn't be having the issue you are describing. 如果您正在触发XHR(ajax)请求,则看到加载图像是有意义的,但是如果您正在执行此操作,那么您就不会遇到正在描述的问题。 What exactly is this javascript task of yours doing? 您的JavaScript任务到底在做什么?

I had a similar issue with a loading image which turned out to be because the image hadn't been loaded into the browser and for whatever reason it didn't display until something else completed. 我在加载图片时遇到了类似的问题,这是因为该图片尚未加载到浏览器中,并且无论出于何种原因,它直到完成其他操作后才显示。 I believe in my case an XHR was somehow blocking the loading or display of the image. 我认为在我的情况下,XHR某种程度上阻止了图像的加载或显示。 From memory, this only happened the first time the loading image was displayed, after that it was fine. 从内存来看,这仅在第一次显示加载图像时才发生,之后就可以了。 I ended up adding an element to the page html to load the loading image and then hid it with javascript. 我最终在页面html中添加了一个元素以加载加载图像,然后用javascript隐藏它。 This solved the problem.. 这样就解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM