如何将动态变量传递给setInterval方法 [英] how to pass the dynamic variable to setInterval method

查看:72
本文介绍了如何将动态变量传递给setInterval方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想每30秒刷新/重新加载一次该方法.但是我无法将变量数据发送到另一个函数,即setInterval.如何在setInterval方法中传递变量.同时添加静态变量的工作原理,但是如何将id传递给setInterval方法

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) 

});

推荐答案

您可以通过多种方式来实现,

you can do it in various way,

示例:

//set data
window.deviceId=Id

并在settimout中使用它

and use it in settimout

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

但是您可以声明settimeout外部作用域的任何全局变量,然后该变量将在settimeout回调函数中可用,因为那样会将其视为Closures

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

这篇关于如何将动态变量传递给setInterval方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆