سؤال

I'm having some issues figuring how to call a function that I pass into a hash as part of another object. So, I have this constructor to create the objects:

function CheckField(jquery_id, test) {
    this.jquery_obj=$(jquery_id);
    this.test=function() {test(this.jquery_obj);};
}

Somewhere else in the code I store these objects with custom test functions for each of them into a hash and iterate through them to set them to be called on an interval:

var fields_to_check = {};
fields_to_check["qt_num_box"] = new CheckField("#qt_num_box", function(qnb){return invalid_quotes(qnb);});
fields_to_check["total_num_lines_box"] = new CheckField("#total_num_lines_box",
                                                        function(tnlb) {
                                                            return isNaN(tnlb()) || 0 >= tnlb.val();
                                                        });
//...
for (var key in fields_to_check) {
    unsafeWindow.console.log("Setting interval for "+key);
    setInterval(set_textbox_border(fields_to_check[key].test, 10000);
}

I'm getting hung up on how to call the custom test function for each of the CheckField objects that I store into fields_to_check.

How can I call the custom test function for each item in the hash?

هل كانت مفيدة؟

المحلول 2

I figured out that I put 10000 in the wrong place, and the first parameter needs to be a function. The call to setInterval should look like this:

setInterval(function() (set_textbox_border(fields_to_check[key].test)), 10000);

نصائح أخرى

You can call the function just like you call any other function:

fields_to_check[key].test();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top