【发布时间】:2015-12-10 12:24:44
【问题描述】:
我正在为心理学研究申请。在某些特定情况下,Web 应用程序需要在很短的时间内(例如 20-50 毫秒)显示图像。
从互联网加载图像不是问题,因为程序会在使用以下脚本进入站点时缓存所有图像。
for stimulus in stimuli
if stimulus
extension = stimulus.split(".").pop()
if extension.match(/png|jpg|jpeg|gif|bmp/i)
# Pre-cache every image to the browser by creating dummy image DOMs
image = $("<img src='#{stimulus}' style='display:none;'></img>")
$(document.body).append(image)
但是,问题如下:当我将图像 DOM 附加到容器时,将立即创建一个计时器函数,在指定时间(如 10 毫秒)后,该函数将删除图像 DOM 并显示下一个图像。当超时时间足够长(> 100 毫秒)时,这非常有效,但有时如果超时时间很短(如 10-50 毫秒),图像就不会显示在屏幕上。我目前的工作是在图像被删除之前应用不透明动画几毫秒。这不仅不是一个好方法,有时(主观观察)图像会显示更长,有时会显示更短。
# Call render function to get the image DOM
$("#stimulus-container").append(stimulus.render(_i + 1, stimuli.length))
if maxTimeout > 0
nextTimer = setTimeout(->
clearTimeout(nextTimer)
# Current (bad) workaround defer the removal of the image DOM
$("#stimulus-container *").animate({
opacity: 0,
}, 50, () ->
# Remove current image and display next
playStimulus(nextSequence)
)
# The designated time to display the image
, maxTimeout
)
我认为问题可能与 DOM 操作的延迟有关。我的代码有什么好的解决方法,还是应该使用其他方法(例如 CSS 动画/画布)重新实现?我是这些(CSS 动画/画布)的新手,因此建议实施的任何细节都将受到高度赞赏。关键是在屏幕上显示图像很短(且稳定)的时间。非常感谢您的关注。
【问题讨论】:
标签: javascript css image performance coffeescript