Reputation: 105
I have a problem with setInterval on a 'click' event. Im already spawning a hidden div. But I want spawn and fade in synced and only run the fade in ones.
var anchor = document.getElementById('anchor');
var hiddenElement = document.getElementById('hiddenElement');
var bkgFade = document.getElementById('bkgFade');
anchor.addEventListener('click', spawnImage, false);
hiddenElement.addEventListener('click', despawnImage, false);
function spawnImage() {
setInterval(function() {
document.getElementById('hiddenElement').style.display = 'block';
document.getElementById('bkgFade').style.visibility = 'visible';
hiddenElement.style.opacity += 1.0;
} 1000);
}
function despawnImage() {
document.getElementById('hiddenElement').style.display = 'none';
document.getElementById('bkgFade').style.visibility = 'hidden';
}
Upvotes: -1
Views: 384
Reputation: 707148
This piece of code makes no sense to me:
function spawnImage() {
setInterval(function() {
document.getElementById('hiddenElement').style.display = 'block';
document.getElementById('bkgFade').style.visibility = 'visible';
F hiddenElement.style.opacity += 1;
});
clearInterval();
}
There are multiple things wrong with it.
setInterval()
.setInterval()
.clearInterval()
. It should be passed the return value from setInterval()
. It won't do anything the way you have it.F hiddenElement.style.opacity += 1;
setInterval()
and then immediately clearInterval()
opacity
directly won't work in some versions of IE.document.getElementById('hiddenElement')
in one place and hiddenElement
in another place. If hiddenElement
is valid, use that both places. If not, use document.getElementById('hiddenElement')
both places.If you describe in more detail what you're really trying to accomplish with this code, folks might be able to help you come up with correct code.
I would highly recommend that animation be done with a library like jQuery or YUI as it's way, way easier to use and make it work cross-browser.
Upvotes: 3