0

i want to refresh/reload the method in every 30 seconds. but im not able to send the variable data to another function i.e setInterval.how to pass variable in the setInterval method.while adding static variable its working but how can i pass the id to setInterval method

 var app = angular.module('PIR_Detection', []);
    app.controller('myCtrl', function ($scope, $http, $window) {
        $scope.sel_val = 0;
        $scope.DefaultLabel = "Loading.....";
        var post = $http({
            method: "get",
            url: "../data.json",
            dataType: 'json',
            data: {},
            headers: { "Content-Type": "application/json" }
        });
        post.success(function (data, status) {
            $scope.Customers = data;
        });
        post.error(function (data, status) {
        });

        $scope.getPIRData = function (id) {
            var url = "/PIRDetails/GetPIRStatus/" + id;


            $http.get(url)
                .then(function (response) {
                    $scope.myWelcome = response.data;
                    $scope.pirstatus = base64toHEX($scope.myWelcome.dataFrame);
                    $scope.timestamp = getIST (response.data.timestamp);
                    $scope.rssi = response.data.rssi;
                    deviceid = id;

                });
        };
        setInterval(function () {
            $scope.getPIRData("100010102");//unable to pass id here
        }, 30000) 

});
krishna mohan
  • 135
  • 1
  • 15
  • So how were you doing it with a varaible? – epascarello Mar 25 '19 at 18:36
  • 1
    In general you should use `$interval` when using AngularJS. `setInterval` will work, but if you get in the habit of using it you'll eventually wonder why your view is not updating in certain circumstances. – Lex Mar 25 '19 at 20:08
  • Also the $http Service is not a drop-in replacement for jQuery ajax. The `dataType` property is ignored, the `application/json` header is unnecessary, and [the `.success` and `.error` methods are deprecated and removed from AngularJS V1.6](https://stackoverflow.com/questions/35329384/why-are-angularjs-http-success-error-methods-deprecated-removed-from-v1-6/35331339#35331339). – georgeawg Mar 25 '19 at 20:39

2 Answers2

1

you can do it in various way,

example:

//set data
window.deviceId=Id

and use it in settimout

setInterval(function () {
            $scope.getPIRData(window.deviceId);//unable to pass id here
        }, 30000)

but you can declare any global variable of the settimeout outer scope, then that variable will be available into the settimeout call back function, because then it will be treated as Closures

Mahamudul Hasan
  • 2,745
  • 2
  • 17
  • 26
  • i did like this document.getElementById("deviceid").innerHTML = id; $scope.getPIRData(document.getElementById("deviceid").innerHTML); can we do it like this? – krishna mohan Mar 25 '19 at 18:46
0

Courtesy of @tvanfosson:

You probably want to have a function that creates the interval timer for you. Pass in the parameter to the function so its value is captured in the function closure and retained for when the timer expires.

function createInterval(f,dynamicParameter,interval) { setInterval(function() { f(dynamicParameter); }, interval); }

Then call it as createInterval(funca,dynamicValue,500); Obviously you can extend this for more than one parameter. And, please, use more descriptive variable names. :)

Alec
  • 8,529
  • 8
  • 37
  • 63