I need to write some code which is supposed to wait until a predefined div
is no longer visible in order to process the next line. I plan on using jQuery( ":visible" )
for this, and was thinking I could have some type of while
loop. Does anyone have a good suggestion on how to acplish this task?
$( document ).ready(function() {
$(".scroller-right" ).mouseup(function( event ) {
alert('right');
pollVisibility();
});
});
function pollVisibility() {
if ($(".mstrWaitBox").attr("visibility")!== 'undefined') || $(".mstrWaitBox").attr("visibility") !== false) {
alert('inside else');
microstrategy.getViewerBone()mands.exec('refresh');
} else {
setTimeout(pollVisibility, 100);
}
}
$( document ).ready(function() {
$(".scroller-right" ).mouseup(function( event ) {
alert('right');
pollVisibility();
});
});
function pollVisibility() {
if (!$(".mstrWaitBox").is(":visible")) {
alert('inside if');
microstrategy.getViewerBone()mands.exec('refresh');
} else {
setTimeout(pollVisibility, 100);
}
}
div
when not visible:
<div class="mstrWaitBox" id="divWaitBox" scriptclass="mstrDialogImpl" dg="1" ty="edt">
</div>
div
when visible:
<div class="mstrWaitBox" id="divWaitBox" scriptclass="mstrDialogImpl" dg="1" ty="edt" visibility="visible">
</div>
I need to write some code which is supposed to wait until a predefined div
is no longer visible in order to process the next line. I plan on using jQuery( ":visible" )
for this, and was thinking I could have some type of while
loop. Does anyone have a good suggestion on how to acplish this task?
$( document ).ready(function() {
$(".scroller-right" ).mouseup(function( event ) {
alert('right');
pollVisibility();
});
});
function pollVisibility() {
if ($(".mstrWaitBox").attr("visibility")!== 'undefined') || $(".mstrWaitBox").attr("visibility") !== false) {
alert('inside else');
microstrategy.getViewerBone().mands.exec('refresh');
} else {
setTimeout(pollVisibility, 100);
}
}
$( document ).ready(function() {
$(".scroller-right" ).mouseup(function( event ) {
alert('right');
pollVisibility();
});
});
function pollVisibility() {
if (!$(".mstrWaitBox").is(":visible")) {
alert('inside if');
microstrategy.getViewerBone().mands.exec('refresh');
} else {
setTimeout(pollVisibility, 100);
}
}
div
when not visible:
<div class="mstrWaitBox" id="divWaitBox" scriptclass="mstrDialogImpl" dg="1" ty="edt">
</div>
div
when visible:
<div class="mstrWaitBox" id="divWaitBox" scriptclass="mstrDialogImpl" dg="1" ty="edt" visibility="visible">
</div>
Share
Improve this question
edited Oct 6, 2016 at 11:15
user247702
24.2k18 gold badges115 silver badges160 bronze badges
asked Sep 28, 2013 at 2:03
user2524908user2524908
8714 gold badges19 silver badges46 bronze badges
6
- Without knowing the context of your problem, I'd say it's better simply edit whatever code is making div visible and have that "process the next line". – Brad Peabody Commented Sep 28, 2013 at 2:08
- Deferred.done() – Brian Commented Sep 28, 2013 at 2:08
- Unfortunately its not that sample, I am extending the functionality of a very large BI application. The javascript which controls this div is very hidden and all the methods are pretty unreadable. At some point the divs viability is changed to visible, I can invoke a custom method at this time using the above code. I need to wait until the div is no longer visible to invoke my next line – user2524908 Commented Sep 28, 2013 at 2:10
- how are you hiding that "div" ? if you are using jQuery to hide it then you can define callback that are execute when hiding its done, see api.jquery./hide/ – Eugen Halca Commented Sep 28, 2013 at 2:11
- I am not hiding the div. There is some native javascript code which is performing that action. I cannot extend the functionality of that method unfortunately. But the native javascript is toggling the visibility – user2524908 Commented Sep 28, 2013 at 2:14
3 Answers
Reset to default 8You can use the setTimeout
function to poll the display status of the div
. This implementation checks to see if the div is invisible every 1/2 second, once the div is no longer visible, execute some code. In my example we show another div, but you could easily call a function or do whatever.
http://jsfiddle/vHmq6/1/
Script
$(function() {
setTimeout(function() {
$("#hideThis").hide();
}, 3000);
pollVisibility();
function pollVisibility() {
if (!$("#hideThis").is(":visible")) {
// call a function here, or do whatever now that the div is not visible
$("#thenShowThis").show();
} else {
setTimeout(pollVisibility, 500);
}
}
}
Html
<div id='hideThis' style="display:block">
The other thing happens when this is no longer visible in about 3s</div>
<div id='thenShowThis' style="display:none">Hi There</div>
If your code is running in a modern browser you could always use the MutationObserver object and fallback on polling with setInterval
or setTimeout
when it's not supported.
There seems to be a polyfill as well, however I have never tried it and it's the first time I have a look at the project.
FIDDLE
var div = document.getElementById('test'),
divDisplay = div.style.display,
observer = new MutationObserver(function () {
var currentDisplay = div.style.display;
if (divDisplay !== currentDisplay) {
console.log('new display is ' + (divDisplay = currentDisplay));
}
});
//observe changes
observer.observe(div, { attributes: true });
div.style.display = 'none';
setTimeout(function () {
div.style.display = 'block';
}, 500);
However an even better alternative in my opinion would be to add an interceptor to third-party function that's hiding the div, if possible.
E.g
var hideImportantElement = function () {
//hide logic
};
//intercept
hideImportantElement = (function (fn) {
return function () {
fn.apply(this, arguments);
console.log('element was hidden');
};
})(hideImportantElement);
I used this approach to wait for an element to disappear so I can execute the other functions after that.
Let's say doTheRestOfTheStuff(parameters)
function should only be called after the element with ID the_Element_ID
disappears, we can use,
var existCondition = setInterval(function() {
if ($('#the_Element_ID').length <= 0) {
console.log("Exists!");
clearInterval(existCondition);
doTheRestOfTheStuff(parameters);
}
}, 100); // check every 100ms