admin管理员组

文章数量:1287918

In the script below I'm planning to have my script polling a device(home automation) for a status. I want to do this every 5 seconds. When I run the script without the loop(setInterval) in runs fine. With the loop it runs fine the first time. The second time I get an error. Im running the script with node.js

First the script:

//import node module request
var request = require('request');

//function to get status of a device
function wrapper (url, device, filter, service) {
request(url, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        obj = JSON.parse(body);
        for (var i = 0; i < obj[device].states.length; i++) {
            if (obj[device].states[i].service === service && obj[device].states[i].variable === filter) {
                deviceStatus = obj[device].states[i].value;
                console.log("deviceStatus inside function: " + deviceStatus);
                //call the pareTime function
                pareTime(deviceStatus);
            }
        }
    }
    return 3;
});
};

function pareTime() {
    var hour = new Date().getHours();
    console.log(hour);
        if ( 8 <= hour && hour <= 21 ) {
            //var deviceStatus = 0;
            console.log("deviceStatus :" + deviceStatus);
            if (deviceStatus === '1') {
                request('url', function (error, response, body) {
                if (!error && response.statusCode == 200) {
                }
                })
                request('url', function (error, response, body) {
                if (!error && response.statusCode == 200) {
                }
                })
            } else {
                console.log("deviceStatus :" + deviceStatus);
            }
        } else {
            console.log("deviceStatus :" + deviceStatus);
        }
};


loop = wrapper('url', 'Device_Num_21', 'Status', 'urn:upnp-org:serviceId:SwitchPower1');

//call the function wrapper with arguments
setInterval(loop, 5000);

The error:

node stack.js
deviceStatus inside function: 1
21
deviceStatus :1

timers.js:261
    callback.apply(this, args);
             ^
TypeError: Cannot call method 'apply' of undefined
    at wrapper [as _onTimeout] (timers.js:261:14)
    at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

Can anyone please assist me on this one?

In the script below I'm planning to have my script polling a device(home automation) for a status. I want to do this every 5 seconds. When I run the script without the loop(setInterval) in runs fine. With the loop it runs fine the first time. The second time I get an error. Im running the script with node.js

First the script:

//import node module request
var request = require('request');

//function to get status of a device
function wrapper (url, device, filter, service) {
request(url, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        obj = JSON.parse(body);
        for (var i = 0; i < obj[device].states.length; i++) {
            if (obj[device].states[i].service === service && obj[device].states[i].variable === filter) {
                deviceStatus = obj[device].states[i].value;
                console.log("deviceStatus inside function: " + deviceStatus);
                //call the pareTime function
                pareTime(deviceStatus);
            }
        }
    }
    return 3;
});
};

function pareTime() {
    var hour = new Date().getHours();
    console.log(hour);
        if ( 8 <= hour && hour <= 21 ) {
            //var deviceStatus = 0;
            console.log("deviceStatus :" + deviceStatus);
            if (deviceStatus === '1') {
                request('url', function (error, response, body) {
                if (!error && response.statusCode == 200) {
                }
                })
                request('url', function (error, response, body) {
                if (!error && response.statusCode == 200) {
                }
                })
            } else {
                console.log("deviceStatus :" + deviceStatus);
            }
        } else {
            console.log("deviceStatus :" + deviceStatus);
        }
};


loop = wrapper('url', 'Device_Num_21', 'Status', 'urn:upnp-org:serviceId:SwitchPower1');

//call the function wrapper with arguments
setInterval(loop, 5000);

The error:

node stack.js
deviceStatus inside function: 1
21
deviceStatus :1

timers.js:261
    callback.apply(this, args);
             ^
TypeError: Cannot call method 'apply' of undefined
    at wrapper [as _onTimeout] (timers.js:261:14)
    at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

Can anyone please assist me on this one?

Share Improve this question asked Oct 12, 2014 at 19:47 CarnifexCarnifex 571 silver badge7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

setInterval() expects its 1st argument to be a function, which loop could be defined as:

function loop() {
    wrapper('url', 'Device_Num_21', 'Status', 'urn:upnp-org:serviceId:SwitchPower1');
}

setInterval(loop, 5000);

The error is because loop is currently holding the undefined value, which can't be treated as a function or object with properties as setInterval() expects.

That value is being returned from wrapper(), which is currently being called immediately.

If you want to call a function with parameters you can do it like so:
setInterval(() => this.yourFunciton(yourParemeter), 100)

本文标签: javascriptsetInterval giving me TypeError Cannot call method 39apply39 of undefinedStack Overflow