I want to run this line of code run first time when function called this.totalVote = this.totalVote - 1;
and after it when second time function run it should not run
我希望第一次运行这行代码时,函数名为this.totalVote = this.totalVote - 1;在第二次运行功能后,它不应该运行
downVote(eve){
this.totalVote = this.totalVote - 1;
if(this.ristricted === this.totalVote){
this.totalVote = this.totalVote - 1;
}else {
}
}
3 个解决方案
#1
0
Set a global variable to control whether or not to run that line:
设置一个全局变量来控制是否运行该行:
var downvoted = false;
downVote(eve){
if(downvoted === false) {
this.totalVote = this.totalVote - 1;
downvoted = true;
}
if(this.ristricted === this.totalVote){
this.totalVote = this.totalVote - 1;
}else {
}
}
#2
0
If you want the function to keep some state (a calledBefore
or callCount
property, for example), you can just add a property to the function itself:
如果您希望函数保持某种状态(例如,calledBefore或callCount属性),您只需向函数本身添加一个属性:
function downVote(eve){
if (!downVote.calledBefore) {
downVote.calledBefore = true;
// do one-time only stuff here:
this.totalVote = this.totalVote - 1;
}
// do other stuff here:
if(this.ristricted === this.totalVote){
this.totalVote = this.totalVote - 1;
}
}
Or you can wrap the function in a closure to hold the state privately:
或者您可以将函数包装在一个闭包中以私有地保存状态:
var downVote = (function (){
var calledBefore = false;
return function downVote(eve) {
if (!calledBefore) {
calledBefore = true;
// do one-time only stuff here:
this.totalVote = this.totalVote - 1;
}
// do other stuff here:
if(this.ristricted === this.totalVote){
this.totalVote = this.totalVote - 1;
}
}
})();
Demo of the latter:
演示后者:
var downVote = (function (){
var calledBefore = false;
return function downVote(eve) {
if (!calledBefore) {
calledBefore = true;
// do one-time only stuff here:
console.log("First call");
}
// do other stuff here:
console.log("Called");
}
})();
downVote();
downVote();
downVote();
#3
0
I guess you could just add another variable to track if the function has been called?
我想你可以添加另一个变量来跟踪函数是否被调用?
this.downVoteCalled = false;
downVote(eve){
if(!this.downVoteCalled){
this.totalVote = this.totalVote - 1;
this.downVoteCalled = true;
}
if(this.ristricted === this.totalVote){
this.totalVote = this.totalVote - 1;
}else {
}
}
#1
0
Set a global variable to control whether or not to run that line:
设置一个全局变量来控制是否运行该行:
var downvoted = false;
downVote(eve){
if(downvoted === false) {
this.totalVote = this.totalVote - 1;
downvoted = true;
}
if(this.ristricted === this.totalVote){
this.totalVote = this.totalVote - 1;
}else {
}
}
#2
0
If you want the function to keep some state (a calledBefore
or callCount
property, for example), you can just add a property to the function itself:
如果您希望函数保持某种状态(例如,calledBefore或callCount属性),您只需向函数本身添加一个属性:
function downVote(eve){
if (!downVote.calledBefore) {
downVote.calledBefore = true;
// do one-time only stuff here:
this.totalVote = this.totalVote - 1;
}
// do other stuff here:
if(this.ristricted === this.totalVote){
this.totalVote = this.totalVote - 1;
}
}
Or you can wrap the function in a closure to hold the state privately:
或者您可以将函数包装在一个闭包中以私有地保存状态:
var downVote = (function (){
var calledBefore = false;
return function downVote(eve) {
if (!calledBefore) {
calledBefore = true;
// do one-time only stuff here:
this.totalVote = this.totalVote - 1;
}
// do other stuff here:
if(this.ristricted === this.totalVote){
this.totalVote = this.totalVote - 1;
}
}
})();
Demo of the latter:
演示后者:
var downVote = (function (){
var calledBefore = false;
return function downVote(eve) {
if (!calledBefore) {
calledBefore = true;
// do one-time only stuff here:
console.log("First call");
}
// do other stuff here:
console.log("Called");
}
})();
downVote();
downVote();
downVote();
#3
0
I guess you could just add another variable to track if the function has been called?
我想你可以添加另一个变量来跟踪函数是否被调用?
this.downVoteCalled = false;
downVote(eve){
if(!this.downVoteCalled){
this.totalVote = this.totalVote - 1;
this.downVoteCalled = true;
}
if(this.ristricted === this.totalVote){
this.totalVote = this.totalVote - 1;
}else {
}
}