admin管理员组文章数量:1392004
So I need to have an image alternatively appear and disappear every 2 seconds, I've been trying using javascript and I've gotten stuck, I feel like it's something so simple but i can't work it out, any help is appreciated.
HTML
<body onload="Show()">
<div align="center">
<img id="image" src="image1.png" height="200" width="200">
JAVASCRIPT
var Cntr=1
function Hide()
{
Cntr++;
document.getElementById("image").style.visibility="hidden";
if (Cntr==2){
setTimeout("Hide", 2000);
}
}
function Show()
{
Cntr--;
document.getElementbyId("image").style.visibility="visible";
if (Cntr==1) {
setTimeout("Show", 2000);
}
}
So I need to have an image alternatively appear and disappear every 2 seconds, I've been trying using javascript and I've gotten stuck, I feel like it's something so simple but i can't work it out, any help is appreciated.
HTML
<body onload="Show()">
<div align="center">
<img id="image" src="image1.png" height="200" width="200">
JAVASCRIPT
var Cntr=1
function Hide()
{
Cntr++;
document.getElementById("image").style.visibility="hidden";
if (Cntr==2){
setTimeout("Hide", 2000);
}
}
function Show()
{
Cntr--;
document.getElementbyId("image").style.visibility="visible";
if (Cntr==1) {
setTimeout("Show", 2000);
}
}
Share
Improve this question
edited May 21, 2014 at 12:01
John Bupit
10.6k9 gold badges44 silver badges79 bronze badges
asked May 21, 2014 at 11:58
MadieMadie
412 gold badges2 silver badges6 bronze badges
2
-
1
It's either
setTimeout(Hide, 2000)
orsetTimeout("Hide()", 2000)
, preferably the former. – JJJ Commented May 21, 2014 at 11:59 -
Even with that fix the logic is off; the first call to
Show()
on page load setsCntr
to 0. You don't actually need the variable at all. – JJJ Commented May 21, 2014 at 12:01
5 Answers
Reset to default 3You need to use callback functions in your setInterval
.
I changed your JavaScript to this:
var isHidden = false;
setInterval(function(){
var el = document.getElementById("image");
el.style.visibility = isHidden ? "visible" : "hidden";
// toggle hidden property
isHidden = !isHidden;
}, 2000);
here is a JSFIDDLE as well.
There are a lot of problems with your code.
As Juhana mentioned, you're using
setTimeout
wrong.Hide()
isn't being called anywhere.
Here's what you can do:
JavaScript
// Store the status of the image. Initially it is 'visible'.
var isVisible = "visible";
function blink() {
// Toggle the position.
if(isVisible == "visible") isVisible = "hidden";
else isVisible = "visible";
// Update it.
document.getElementById("image").style.visibility = isVisible;
// Repeat the process every 2 seconds.
setTimeout(blink, 2000);
}
HTML
<body onload="blink()">...</body>
Working example.
in you setTimeout()s swap show and hide. Also, remove var Cntr. wont need it
function Hide()
{
document.getElementById("image").style.visibility="hidden";
setTimeout("Show()", 2000);
}
function Show()
{
document.getElementbyId("image").style.visibility="visible";
setTimeout("Hide()", 2000);
}
Try this -
$(function($) {
setInterval(function() {
if($('#image').is(':visible'))
$('#image').hide();
else
$('#image').show();
}, 2000);
});
Use jQuery instead of raw JS, it is more clean and simple. Use Delay and FadeTo functions with recursion and you are done with one simple function. Call this on page load.
function Flicker(){
$("#MyImage").delay(1500).fadeTo(300,0).delay(1500).fadeTo(300,1, Flicker);
}
Where: #MyImage is ID of image.
You can see this working @ http://jsfiddle/BM3qK/1/
Hope this helps.
本文标签:
版权声明:本文标题:javascript - HideShow image after 2 seconds - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744426851a2605737.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论