你知道有什么javascript api有很多前提处理吗?就像人工智能一样
JQuery可以处理实现观察者设计模式的事件,但对于一个回调函数,它只能处理一个事件 我想知道,它是否可以处理许多事件:当所有这些事件被触发时,或者当所有布尔前提变为真时,调用函数 开发动态应用程序会更好 你知道这样的事情是否已经存在了吗? 如果没有:你认为如果我发展会很好吗 编辑: 我想这样做:你知道有什么javascript api有很多前提处理吗?就像人工智能一样,javascript,jquery,artificial-intelligence,Javascript,Jquery,Artificial Intelligence,JQuery可以处理实现观察者设计模式的事件,但对于一个回调函数,它只能处理一个事件 我想知道,它是否可以处理许多事件:当所有这些事件被触发时,或者当所有布尔前提变为真时,调用函数 开发动态应用程序会更好 你知道这样的事情是否已经存在了吗? 如果没有:你认为如果我发展会很好吗 编辑: 我想这样做: when((a==b && c!=0), function(){ //do something when a==b and c!=0 alert("a==b && c
when((a==b && c!=0), function(){
//do something when a==b and c!=0
alert("a==b && c!=0");
});
obj.watch(function(){
alert("obj changes");
});
var myFlagger = new flagger(
/* The amount of flags that need to be set: */8,
/* The function that needs to be called when the flags are all set: */
function () { alert('Callback function called'); },
/* If the callback function should be called again when a flag is unflagged and reflagged.
Optional. Default: false */false);
// You can now use these functions:
myFlagger.flag(0); // Use this to set a flag, index ranges from 0 to count - 1.
myFlagger.unflag(0); // Unflags an index.
myFlagger.reset(); // Resets all flags to false.
myFlagger.reset(true); // Using this forces the reset, returning to full start
// state, causes callback function to be called again
// even if 'once' is specified.
alert('0 is flagged: ' + myFlagger.isFlagged(1)); // Returns if flag is flagged.
编辑2:
我发现了一个很棒的API,它允许监听变量更改。你只需要这样做:
when((a==b && c!=0), function(){
//do something when a==b and c!=0
alert("a==b && c!=0");
});
obj.watch(function(){
alert("obj changes");
});
var myFlagger = new flagger(
/* The amount of flags that need to be set: */8,
/* The function that needs to be called when the flags are all set: */
function () { alert('Callback function called'); },
/* If the callback function should be called again when a flag is unflagged and reflagged.
Optional. Default: false */false);
// You can now use these functions:
myFlagger.flag(0); // Use this to set a flag, index ranges from 0 to count - 1.
myFlagger.unflag(0); // Unflags an index.
myFlagger.reset(); // Resets all flags to false.
myFlagger.reset(true); // Using this forces the reset, returning to full start
// state, causes callback function to be called again
// even if 'once' is specified.
alert('0 is flagged: ' + myFlagger.isFlagged(1)); // Returns if flag is flagged.
您可以轻松地手动执行此操作:
var flag1 = false;
var flag2 = false;
var flag3 = false;
var checkAllFlags = function () {
if (!flag1 || !flag2 || !flag3) return;
checkAllFlags = function () { };
allFlagsSet();
}
var allFlagsSet = function () {
// Execute here when all flags are set
}
// When you set a flag do it like this:
flag2 = true;
checkAllFlags();
或者您可以使用这个类:
window.flagger = function (count, callback, once) {
var curr = this;
curr.callback = callback;
if (once)
curr.called = false;
curr.flags = [];
curr.flagsLeft = count;
for (var i = 0; i < count; i++)
curr.flags[i] = false;
this.flag = function (index) {
if (!curr.flags[index]) {
curr.flags[index] = true;
if (--curr.flagsLeft <= 0 && (!once || !curr.called)) {
if (once) curr.called = true;
curr.callback();
}
}
};
this.unflag = function (index) {
if (curr.flags[index]) {
curr.flags[index] = false;
curr.flagsLeft++;
}
};
this.reset = function (force) {
for (var i = 0; i < count; i++)
curr.flags[i] = false;
curr.flagsLeft = count;
if (once && force)
curr.called = false;
};
this.isFlagged = function (index) {
return curr.flags[index];
};
}
我希望这能对你有所帮助。对我来说,这听起来像是个笑话。用这个概念开发的大多数库(JavaScript有很多库,包括jQuery.deferred)可以简单地为其他一些实现的事件构建一个新的承诺。但是,当变量值更改时,我必须验证所有变量。我想知道更像是一个观察者,观察前提中的每个变量,当主题值改变,然后验证其他前提如果都为真,然后调用回调函数我不知道jQuery中有这样的功能,抱歉:/@user858257逻辑也可以使用二进制标志和位掩码执行,但我不确定这是否会更容易。我正在阅读$.deffered,它真的很有用,通过一些工作,它可能就是我想要的,谢谢。我认为jQuery的promise类不是最强大的,但是如果您已经使用jQuery,它可能是dojo的替代品,或者实际上,jQuery的promise和deferreds类并没有那么酷!我没有找到使用$的方法。当我的应用程序逻辑发生某些事情时,就在jquery api发生某些事情时,比如$ajax。您可以在应用程序逻辑中创建自己的。在他们身上调用$。应该可以完成此任务。但正如我所说的,jQuery并不是最强大或最有文档记录的promise库。我更新了答案,也许这会有更多帮助?