I am working on an application for psychology researches. In some specific cases, the web application need to display an image for a very short amount of time (like 20-50ms).
Loading the image from the internet is not an issue as the program will cache all images when entering the site with following script.
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)
However, the problem is as following: when I append the image DOM to the container, a timer function will be immediately created, after designated time (like 10ms) the function will remove the image DOM and display next image. This works perfectly when the timeout is long enough (> 100ms), but sometimes the image just won't display on the screen if the timeout is very short (like 10-50ms). My current work around is apply a opacity animation for few milliseconds before the image has been removed. Not only this is not a good method, sometimes (subjective observation) the image will display longer and sometime it displays shorter.
# 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
)
I believe the problem may be related to the latency of DOM operations. Is there any good workarounds for my code, or should I use other approaches such as CSS animation / Canvas to reimplement? I am new to these (CSS animation / Canvas) so any detail suggested for implementation would be highly appreciated. The key point is to display an image on screen for a very short (and stable) period of time. Thank you very much for your attention.