Forum Moderators: open
var selector = $('.someClass'),
i = 0;
selector.each(function() {
i += 1;
// Some stuff
if(i == selector.length) {
// "callback"
}
});
var objectLength = Object.keys(myObject).length;
var a = function() {};
a();
[edited by: Readie at 1:05 pm (utc) on Jul 30, 2013]
(function(jQuery) {
})(jQuery);
var a = '1';
var b = '2';
var c = '3';
(function(a, b) {
alert(a + ' ' + b); // Outputs '2 3'
})(b, c);
alert(a + ' ' + b); // Outputs '1 2'
(function($) {
$.fn.eachWithCallback = function() {
var $this = {};
$this.length = $(this).length;
$this.hits = 0;
$this.primaryMethod = arguments[0];
$this.callback = arguments[1];
return this.each(function() {
var i = 0;
var key = 'cb_' + i;
while(typeof this[key] != 'undefined') {
i += 1;
key = 'cb_' + i;
}
this[key] = $this.primaryMethod;
this[key]();
delete this[key];
$this.hits += 1;
if($this.hits == $this.length) {
$this.callback();
}
});
}
$('a').eachWithCallback(function() {
$(this).html('test');
}, function() {
alert('done');
});
})(jQuery);
[edited by: Readie at 5:14 pm (utc) on Jul 30, 2013]
1. Is this supposed to replace my function (with minor edits to do the actions I need) or to be used along with my function. I assumed it's the former, and tried to merge the two w/o success.
2. You don't include arguments in your function, yet you rely on them inside; I assume you left them out for brevity and included them like so:
$.fn.eachWithCallback = function(arg1, arg2, callback)
Is that correct?
function a() {
alert(arguments[0] + ' ' + arguments[2]);
}
a('fus', 'ro', 'dah'); // Outputs 'fus dah'
3. You have:
$this.primaryMethod = arguments[0];
$this.callback = arguments;
but I need 2 args plus the callback; I changed line 2 to:
$this.callback = arguments[2];
but how to handle line 1?
My console log says only: "LOG: [object Arguments]" -- is that correct?
4. (maybe related to #3) I get this error:
"The value of the property 'cb_0' is not a Function object"
5. You close eachWithCallback() with a simple curly bracket; my function closes with the bracket plus semi-colon -- does it matter? (I tried both; still doesn't work)
6. I don't understand your test call; I have this simple code (tweaked to accomodate your fn):
$("#test").eachWithCallback(1500, 2500, 'callback_func');
[1][edited by: Readie at 8:05 pm (utc) on Jul 30, 2013]
var myCallbackVariable = function() {}
//....
// Nested in several functions etc
//...
$('.someSelector').eachWithCallback(function() {
// Usual $.each function
}, myCallbackVariable);
@Dave:
$.each spawns child threads, and then the parent thread just carries on without waiting for those child threads to finish
$('a').each(function() {
var i = null;
while(i != '0.55555') {
i = String(Math.random().toFixed(5));
}
console.log(this);
});
console.log('done');
<a title="Some Page 1" href="/some_page1">
jquery....min.js (line 3)
<a title="Some Page 2" href="/some_page_1/some_page_2">
jquery....min.js (line 3)
<a title="Some Page 3" href="/some_page_1/some_page_2/some_page_3">
jquery....min.js (line 3)
<a title="Sitemap" href="/sitemap">
jquery....min.js (line 3)
done