静态javascript变量用作Angularjs控制器中的计数器 [英] Static javascript variable to be used as counter in Angularjs controller
本文介绍了静态javascript变量用作Angularjs控制器中的计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个静态javascript变量用作Angularjs控制器内的计数器.此静态变量将在重复调用的轮询函数中使用.
I would like to create a static javascript variable to be used as a counter inside a Angularjs controller. This static variable will be used inside a polling function that gets repeatedly called.
我想以这种方式使用静态变量;
I want to use the static variable in a manner that looks like this;
var polling_func = function()
{
static var counter = 0;
if (counter == 10)
{
alert('Do action');
counter = 0;
}
counter = counter + 1;
$timeout(polling_func, 1000);
}
polling_func();
不幸的是,我无法在javascript中使用static关键字声明静态变量.我应该如何在我的代码中这样做?
Unfortunately, I cannot declare a static variable using static keyword in javascript. How should I go about doing so in my code?
推荐答案
为什么不声明全局变量,因此无论何时调用函数,它都不会更改值.
Why not declare a global variable, so it will not change the value whenever function is called.
var counter = 0;
var polling_func = function()
{
if (counter == 10)
{
alert('Do action');
counter = 0;
}
counter = counter + 1;
}
polling_func();
$timeout(polling_func, 1000);
这篇关于静态javascript变量用作Angularjs控制器中的计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文