0

I don't know why my function in the console shows not defined. I have tried to make it right and I just can't seem to get it. It works fine until I try to use the setInterval function. Also the build keeps saying I am missing a semicolon, but I just don't see it.

$(document).ready(function () {

    var machineDataViewModel = {
        machineDataItems: ko.observableArray([]),

        loadMachineDataItems: function DataLoad() {

            machineDataViewModel.machineDataItems.length = 0;

            $.getJSON("http://localhost/JsonRestful/Service1.svc/GetMachineData", function (data) {


                $.each(data.GetMachineDataResult, function (index, item) {
                    machineDataViewModel.machineDataItems.push(new machineDataModel(item));

                });
            });
        }
    };

    ko.applyBindings(machineDataViewModel);

    machineDataViewModel.loadMachineDataItems();
    setInterval(DataLoad, 9000);
});

function machineDataModel(item) {
    this.mach_no = ko.observable(item.mach_no),
    this.VAR1 = ko.observable(item.VAR1),
    this.VAR2 = ko.observable(item.VAR2),
    this.VAR3 = ko.observable(item.VAR3),
    this.VAR4 = ko.observable(item.VAR4)
};
4
  • 1
    In your code, there is no definition of the variable "ko". In your "machineDataModel", the line that starts with "this.VAR4" needs to end with a semicolon. Commented Oct 30, 2014 at 21:16
  • @ray9209 - yes, it is too a function. It's a property that contains a function reference. Commented Oct 30, 2014 at 21:24
  • All of the this.XXXX.... with the commas make no sense. There is no reason you should be using commas. It is not the typically var xxx,xxxx,xxxxx; pattern. Commented Oct 30, 2014 at 21:29
  • @epascarello You're correct that the commas are poor style, but they'll happen to function as expected since the comma operator has lower precedence than the assignment operator. Not many operators can boast such low precedence! Commented Oct 30, 2014 at 21:55

1 Answer 1

3

You can't define the DataLoad() function the way you are and expect it to be available in your setInterval(). It simply doesn't work that way. The symbol DataLoad is only available inside the scope of that function. Instead, you can call it as:

setInterval(machineDataViewModel.loadMachineDataItems, 9000);

Here's a simple demonstration that shows you can't name your function like you are and expect to use that name outside that scope: http://jsfiddle.net/jfriend00/6t0pp60s/ (look in the debug console to see the error).


FYI, if you need your function to have the right value of this (which I don't think you actually do), then you would call it like this (with a semi-colon at the end of every line):

setInterval(machineDataViewModel.loadMachineDataItems.bind(machineDataViewModel), 9000);

As for the semicolon issue, jsHint points to the this.VAR4 assignment line. I'd suggest changing machineDataModel() to this (which gives you a clean bill of health in jsHint):

function machineDataModel(item) {
    this.mach_no = ko.observable(item.mach_no);
    this.VAR1 = ko.observable(item.VAR1);
    this.VAR2 = ko.observable(item.VAR2);
    this.VAR3 = ko.observable(item.VAR3);
    this.VAR4 = ko.observable(item.VAR4);
}
Sign up to request clarification or add additional context in comments.

3 Comments

It should be more instructive to suggest setInterval(function () { machineDataViewModel.loadMachineDataItems() }, 9000); and then mention bind as a possible shortcut to that. Good idea bringing up jsHint. The sooner someone starts using that, the better their life with JS will be.
@Cory - I thought it worth educating on .bind() as this is exactly what it is for. Not sure why a function stub would be recommended before that?
Someone who's just learning about bind might understand it better if they first see the more verbose code that accomplishes the same task. (Oddly, the variable name is so long here that the function expression is actually less verbose, but that shouldn't really be a deciding factor.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.