English 中文(简体)
JavaScript 运行前的 CSS (显示进度指标)
原标题:Repaint CSS before JavaScript runs (to show progress indicator)

我有一个JS的例行程序( 用户触发) 需要一些时间才能完成 。 虽然例行程序正在运行, 我要在屏幕上显示进度覆盖 。 以下是调用例行程序的代码( 这是响应点击事件而调用 ) :

function handleClick() {
    $( div#progressOverlay ).removeClass( hidden );
    myBigRoutine();
    ...
    $( div#progressOverlay ).addClass( hidden );
}

类切换会触发 obcity visibility 的更改(随转换而变化)。

班级改变本身运作良好;第一种在慢程序之前执行,第二种则在所有其他情况下执行。

The issue is that the visual appearance of #progressOverlay doesn t change until after myBigRoutine() finishes.
The result is that the progress overlay flashes on screen for a split second and then is immediately hidden again (all with no animation)

是否有办法强制在大型 JavaScript 常规之前(或更好,

最佳回答

您可以将事件绑绑到动画完成 。 因此, 当动画完成时, 只有这样, 您的代码才会执行,

function handleClick() {
    $( div#progressOverlay ).removeClass( hidden );

}

$("div#progressOverlay").bind("animationend webkitAnimationEnd oAnimationEnd MSAnimationEnd", function(){
    myBigRoutine();
    ...
    $( div#progressOverlay ).addClass( hidden );
});

我希望,这就是你需要的:)

问题回答

您的 < code> myBigRoutine () 函数应该以非同步方式被调用 。

function handleClick() {
  $( div#progressOverlay ).removeClass( hidden );
  setTimeout(function() {
    myBigRoutine();
    ...
    $( div#progressOverlay ).addClass( hidden );
  }, 150);
}

150是一个神奇数字,这意味着超时延迟以毫秒计。它可以是任何小数字。

另外,考虑使用 .hide () .show () jQuery 方法,而不是 hidden 类。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签