Reputation: 1661
how i can stop a function from repeating if the div html = Ready, this is my code
(function(){
var Load_Div_Error = $( ".Load_Div_Error" ).html();
if(Load_Div_Error == "Ready") {
$( "div.Load_Div_Error" ).text( "Test" );
}
setTimeout(arguments.callee, 2000);
})();
Upvotes: 1
Views: 134
Reputation: 15356
jQuery's .html()
may return whitespace around "Ready". remove it before comparing..
(function myLoad(){
var Load_Div_Error = $( ".Load_Div_Error" ).html();
if($.trim(Load_Div_Error) == "Ready") // remove the whitespace before comparing
$( "div.Load_Div_Error" ).text( "Test" );
else
setTimeout(myLoad, 2000); // run again only if Load_Div_Error != 'Ready'
})();
I've replaced arguments.callee
with a named function because it is forbidden in strict mode - better not to use it
Upvotes: 2
Reputation: 6217
You can set the Timeout
to an variable and clear it after it is no longer needed:
(function(){
var timer;
var Load_Div_Error = $( ".Load_Div_Error" ).html();
if(Load_Div_Error == "Ready") {
$( "div.Load_Div_Error" ).text( "Test" );
clearTimeout(timer);
}
else
timer = setTimeout(arguments.callee, 2000);
})();
Or, I'm thinking of clearing an Interval when using the setInterval
function, which would be better to use instead of setTimeout
, IMHO.
But yeah, if you just add an else statement and place the setTimeout
in there, it would work as well, since setTimeout
doesn't need to be cleared.
Upvotes: 2
Reputation: 39777
Add else
block to your if
:
if(Load_Div_Error == "Ready") {
$( "div.Load_Div_Error" ).text( "Test" );
} else {
setTimeout(arguments.callee, 2000);
}
This was setTimeout
will kick on only if Div content != Ready
Upvotes: 2