告诉JS代码每次调用它只能运行一次? [英] Tell the JS code to only function once per time its called?

查看:144
本文介绍了告诉JS代码每次调用它只能运行一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在这里和那里写了一些代码部分,我正试图开发一个游戏,并且我已经编写了一个片段来在条件满足时引发动作,但是一旦条件满足,动作就会无限循环。我需要它只针对遇到的条件触发一次或者要求它只运行一次?

对不起我是一个至高无上的新手并且还在学习,几周前才用Java脚本开始。



更深入..

当IE:目标A命中目标B时满足条件。

然后下面的部分执行并提示目标A使用技能X,但这也算作命中并导致条件再次被满足并因此无限循环。

请如何阻止它。怎么写的?这可以用新手来解释吗?



这就是我所拥有的。



var skill = 49 ;

var target = -2;

BattleManager.queueForceAction(用户,技能,目标);



注意:用户在别处定义

我是否正确解释了这个问题?



我尝试了什么:



我尝试创建并存储一个变量来生成if条件(时间< = 1)但可能没有写得正确所以它可能没有正常工作..

这是我试过的街区



var times = 0;

var skill = 49;

var target = -2;

if(times< 1){

BattleManager.queueForceAction(用户,技能,目标);

次= +1;

}

So I am writing some code sections here and there for a game I'm trying to devellope and I have written a piece to cause an action when a condition is met, however the action loops infinitely once the condition is met. I need it to only trigger once per condition met or to ask it to only run once?
Sorry I am a supreme novice and still learning, only started a couple weeks ago with Java script.

More in depth..
The condition is met when IE: target A "Hits" target B.
Then the below piece executes and prompts target A to use skill X, but this also counts as a "Hit" and causes the condition to be met again and thus infinite loop.
Please how do I stop this.. How is it written? Can this be explain in novice terms?

This is what I have.

var skill = 49;
var target = -2;
BattleManager.queueForceAction(user, skill, target);

NOTES: "user" is defined elsewhere
Did I explain this correctly?

What I have tried:

I tried creating and storing a variable to make an if condition of (times <= 1) but may not have written that correctly so it may not have worked right..
This is the block I tried

var times = 0;
var skill = 49;
var target = -2;
if (times < 1) {
BattleManager.queueForceAction(user, skill, target);
times = +1;
}

推荐答案

在JavaScript中,几乎与任何语言一样(我能想到)没有例外,无论如何),每次调用只调用一次函数。如果你有一个无限循环,那么你有一个不同的问题。



你发布的任何内容都包含任何类型的循环(for,while,foreach)所以代码你发布的内容包含在一个循环中,你调用的代码(queueForceAction)包含一个被锁定的循环,或者你的某个地方有一个递归调用。



JS中的一个很好的故障排除技术是将console.log()语句塞进代码段,以帮助您诊断存在问题的位置。
In JavaScript, like pretty much any language (I can think of no exceptions, at any rate), a function is invoked only once for each call. If you have an infinite loop, then you have a different issue.

Nothing you posted includes a loop of any sort (for, while, foreach) so either the code you posted is contained in a loop, there code you call (queueForceAction) contains a loop that is getting locked, or you have a recursive call tucked in there somewhere.

One good troubleshooting techniques in JS is to tuck console.log() statements into sections of code to help you diagnose where issues exist.


这篇关于告诉JS代码每次调用它只能运行一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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