开发者

$("article").filter*(while).each(repeat_me) - loop while filter matches

开发者 https://www.devze.com 2023-01-13 02:55 出处:网络
I\'d like to have a custom function repeatedly called as long as a custom filter function matches. use case:

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 .fil开发者_C百科ter() matches.


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.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消