Skip to content

Instantly share code, notes, and snippets.

@ncr
Created December 10, 2008 19:36
Show Gist options
  • Save ncr/34453 to your computer and use it in GitHub Desktop.
Save ncr/34453 to your computer and use it in GitHub Desktop.
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js"></script>
<script type="text/javascript">
(function($) {
$.fn.at_intervals = function(code, interval) {
return this.each(function() {
var it = this // this is it :)
var interval_id = setInterval(function(){
if($(it).parents("html").length == 1) {
console.log("codes")
code();
} else {
console.log("kill -9")
clearInterval(interval_id);
}
}, interval);
});
};
})(jQuery);
</script>
</head>
<body>
<h1>FF + FireBug. Observe console</h1>
<a href="#" id="quit">Quit (remove the widget from DOM)</a>
<script type="text/javascript">
$("#quit").click(function(){
$("#tagsoup1").empty()
});
</script>
<div id="tagsoup1">
<div id="tagsoup2">
<div id="widget">
<p>Widget with setIntervals. Self-contained piece of code and looks.</p>
<div id="logic" style="background-color: red; width: 66px; height: 66px"></div>
<script type="text/javascript">
$("#widget").at_intervals(function(){
$("#logic").toggle(200)
}, 1000);
</script>
</div>
</div>
</div>
</body>
</html>
(function($) {
$.fn.at_intervals = function(codes, interval) {
return this.each(function() {
var it = this // this is it :)
var interval_id = setInterval(function() {
// check if "it" is still in the DOM
if($(it).parents("html").length == 1) {
codes();
} else {
clearInterval(interval_id);
}
}, interval);
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment