danyo
danyo

Reputation: 5846

javascript alert when element is visible

i have the following code:

var blink1 = function() {
    $('.leftArrowMask').hide();
    setTimeout(blink2, 5000);
};
var blink2 = function() {
    $('.leftArrowMask').show();
    setTimeout(blink1, 1000);
};
$(document).ready(function() {
    setTimeout(blink1, 1000);
});

this basically shows a div for 1 second, and then hides it for 5 seconds.

I would like to display an alert everytime the div is visible.

i have tried the following but it doesnt seem to work:

function checkVisibility(){
  if ($('.leftArrowMask').is (':visible') && $('.leftArrowMask').parents (':hidden').length == 0)
  alert ("Visible!");
   setTimeout('checkVisibility',1000)//every 1 second...
}

any ideas on what could be wrong?

Upvotes: 0

Views: 2334

Answers (3)

Neeraj
Neeraj

Reputation: 4489

 var blink1 = function () {
        $('.leftArrowMask').hide();
        setTimeout(blink2, 5000);
    };
    var blink2 = function () {
        $('.leftArrowMask').show();
        setTimeout(blink1, 1000);
    };
    $(document).ready(function () {

        setInterval(function () {
            if ($('.leftArrowMask').is(':visible') && $('.leftArrowMask').parents(':hidden').length == 0) {
                alert("Visible!");
            }
            setTimeout(blink1, 1000);
        }, 1000);

        //
    });

See Demo

Hope helps

Upvotes: 1

L105
L105

Reputation: 5419

You could only set a setInterval somewhere in your code.

setInterval(function() {
    if ($('.leftArrowMask').is (':visible') && $('.leftArrowMask').parents (':hidden').length == 0) {
        alert ("Visible!");
    }
}, 1000);

Upvotes: 0

Praveen
Praveen

Reputation: 56501

setTimeout('checkVisibility',1000)//every 1 second...

This is wrong, remove single quotes (')

setTimeout(checkVisibility,1000);

Upvotes: 0

Related Questions