tags:

views:

30

answers:

2

I'd like to have a custom function repeatedly called as long as a custom filter function matches.

use case:

// filter
function overflow() { 
    return this.clientHeight < this.scrollHeight;
}

// apply
$("article").filter(overflow).css_calc({fontSize: -1});

Is there a generic way in jQuery to call the subsequent function as long as the filter matches? Or wasn't there something to call the preceeding function multiple times? Any way to loop in a jQuery function chain?

Or otherwise; how would you write something like an .each_while() combo that does the usual .each() but also performs a while() on each element as long as said .filter() matches.

+2  A: 

Use setInterval to periodically call your code at regular intervals.

function update() {
    $("article").filter(overflow).css_calc({fontSize: -1});
}

var timer = setInterval(update, 200); // update every 200ms

To clear the update, call

clearInterval(timer);
Anurag
+1 intriguing *workaround* :}
mario
+1  A: 

I may not fully understand your question, but I think you can get with you want using simple imperative programming:

// filter
function overflow() { 
    return this.clientHeight < this.scrollHeight;
}

while($("article").filter(overflow).length > 0)
    $("article").filter(overflow).css_calc({fontSize: -1});

A more elegant solution escapes me at the moment, though.

Ian Henry
Indeed, that while does the trick. (lol, I was too immersed in jQuery semantics and searching for an elaborate high-level solution.)
mario