admin管理员组

文章数量:1287579

I started working on advanced java few days before(too late to start on that, I know). I am stuck with a specific task of making an icon (which is present on the task bar) blink. This blinking should happen based on a specific condition, which means that it can be achieved using javascript.

I have been searching for a while now but is there a way to make an icon appear and disappear every 1 second or so to bring in the blinking effect ?

I started working on advanced java few days before(too late to start on that, I know). I am stuck with a specific task of making an icon (which is present on the task bar) blink. This blinking should happen based on a specific condition, which means that it can be achieved using javascript.

I have been searching for a while now but is there a way to make an icon appear and disappear every 1 second or so to bring in the blinking effect ?

Share Improve this question edited Apr 30, 2013 at 3:53 Andrew Thompson 169k41 gold badges222 silver badges436 bronze badges asked Apr 30, 2013 at 3:51 Anuj BalanAnuj Balan 7,72923 gold badges61 silver badges94 bronze badges 11
  • If you can do it in HTML, you can do it in a JSP, therefore this has nothing to do with Java, JSP or (polite cough) 'advanced Java'. – Andrew Thompson Commented Apr 30, 2013 at 3:54
  • Tagged adv.java because jsp, javascript, etc es under that title. – Anuj Balan Commented Apr 30, 2013 at 3:55
  • 1 JSP is not especially advanced, and Javascript is NOT Java. – Andrew Thompson Commented Apr 30, 2013 at 3:56
  • Javascript is not java and it is a known fact. I never mentioned so anywhere. Adv. java = jsp+servlets+javascript+..., we were taught so.. – Anuj Balan Commented Apr 30, 2013 at 3:59
  • 1 The code depends on the exact effect you're going for, but probably the easiest solution is to put the icon where you want on the page using HTML and CSS, and then using the JavaScript setInterval function to change the visibility (i.e., "visible" of "hidden") every so many milliseconds. – HartleySan Commented Apr 30, 2013 at 4:00
 |  Show 6 more ments

3 Answers 3

Reset to default 5

HTML

<img src='image/source' alt='blinking!' id='blinking_image' />

Javascript

var img = document.getElementById('blinking_image');

var interval = window.setInterval(function(){
    if(img.style.visibility == 'hidden'){
        img.style.visibility = 'visible';
    }else{
        img.style.visibility = 'hidden';
    }
}, 1000); //the 1000 here is milliseconds and determines how often the interval should be run.

This creates an anonymous function inside the setInterval that runs every 1 second (1sec == 1000milisec). To see more about setInterval checkout the mdn here on it.

Each time it runs it checks to see if the img is hidden or visible if it's hidden then it shows it if it's visible then it hides it. It does this by checking the style.visiblity property. Which you can learn more about here on the mdn.

Small fix

instead

if(img.display == 'hidden')

use

if(img.style.visibility == 'hidden')

You might find opacity works better because the image is still there, which means it is still clickable if necessary. Also you can add a clear interval to stop the flashing.

var mycounter = 0
interval = window.setInterval(function () {
if (img.style.opacity == '0.1') {
            img.style.opacity = '1';

            mycounter = mycounter + 1
            if (mycounter == 7) {
                clearInterval(interval);
            }
        } else {
            img.style.opacity = '0.1';
        }
    }, 500); //the 1000 here is milliseconds and determines how often the interval 

本文标签: javascriptHow to make an icon flashblinkwhich is present in a web pageStack Overflow