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) or setTimeout("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 sets Cntr to 0. You don't actually need the variable at all. – JJJ Commented May 21, 2014 at 12:01
Add a ment  | 

5 Answers 5

Reset to default 3

You 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.

  1. As Juhana mentioned, you're using setTimeout wrong.

  2. 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.

本文标签: