Skip to content

Instantly share code, notes, and snippets.

@droot
Created February 26, 2012 17:01
Show Gist options
  • Save droot/1917722 to your computer and use it in GitHub Desktop.
Save droot/1917722 to your computer and use it in GitHub Desktop.
Use of Async IF construct
/* function to detect if a box of given color is visible on screen or not */
is_box_visible = function(color) {
var selector;
selector = "." + color + "box:visible";
return $(selector).length > 0;
};
$.when(async_if(is_box_visible, 'green') /* green box is visible */,
async_if(is_box_visible, 'red') /* red box is visible */
).then(
function() {
/* when both the red and green box appeared, inject a blue box */
inject_box('blue');
}
);
/* a timeout sample usecase, detect a redbox within 10seconds of start */
async_if(is_box_visible, 'red', 10000).then(
function(color) {
alert('red box detected');
},
function(status) {
alert("couldn't detect redbox 10 seconds....");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment