根据 if 语句的结果更改 if 语句参数中变量的值
Change value of variable in a if-statment argument depending of the if-statments outcome
我想做的是每五秒联系一次 WooCommerce,以检查产品是否已被编辑。我可以通过请求 res[0].date_modified 来做到这一点。然后我比较它的创建时间(res[0].date_created
)和最后修改时间。
如果值相同,则该产品从未被修改过。但是,如果产品被修改,我不再想比较产品的创建时间和上次修改时间。
我现在想将时间与上次使用“res[0].date_modified
”修改的时间进行比较,以便我可以查看它是否在另一次被修改。
这是我想出的:
function lookForEdit(){
WooCommerce.get('products/', function(err, WooData, res) {
res=JSON.parse(res)
var comp = res[0].date_created;
if(comp == res[0].date_modified){
console.log("the product haven't been modified")
}
else{
console.log("The product have been edited")
comp = res[0].date_modified;
}
})
}
setInterval(lookForEdit, 5000);
我明白为什么它不起作用,但我不知道如何修复它。
我该如何解决?
谢谢
嗯,马上,你设置 comp = res[0].date_created
然后,如果它们相等,你设置 comp = res[0].date_modified
但是下一次你调用 api,您正在用再次创建的日期覆盖 comp
。您应该使用 2 个单独的变量来跟踪信息。您应该为您尝试监控的值使用 2 个单独的更具描述性的名称。
使用全局变量存储最后一个值就足够了,只要先检查它是否被定义,如果没有就取date_create
值。
当然可以勾选date_modifed
只有在没有为未修改的项目定义的情况下才可以勾选
在这种情况下,您只需要检查是否定义了 date_modifed
值,如果是,则与 comp
(仍然是全局的)进行比较。然后如果有不同的存储新值。
PS:对不起,我没有写javascript遗嘱,所以不能写代码。我最终错误地回答了那个问题^^
这将是它的伪代码
global comp;
function(){
if(comp not defined)
comp = date_created
if(comp != date_modified)
//changed
comp = date_modified;
}
由于您的代码每 5 秒运行一次,我想您可以检查最后一次修改是否发生在不到 5 秒之前:
if(comp == res[0].date_modified){
console.log("the product hasn't been modified")
}
else if (new Date(Date.now().getTime() - 5000) < res[0].date_modified) {
console.log("the product hasn't been modified since the last check");
}
else{
console.log("The product has been edited")
comp = res[0].date_modified;
}
我想做的是每五秒联系一次 WooCommerce,以检查产品是否已被编辑。我可以通过请求 res[0].date_modified 来做到这一点。然后我比较它的创建时间(res[0].date_created
)和最后修改时间。
如果值相同,则该产品从未被修改过。但是,如果产品被修改,我不再想比较产品的创建时间和上次修改时间。
我现在想将时间与上次使用“res[0].date_modified
”修改的时间进行比较,以便我可以查看它是否在另一次被修改。
这是我想出的:
function lookForEdit(){
WooCommerce.get('products/', function(err, WooData, res) {
res=JSON.parse(res)
var comp = res[0].date_created;
if(comp == res[0].date_modified){
console.log("the product haven't been modified")
}
else{
console.log("The product have been edited")
comp = res[0].date_modified;
}
})
}
setInterval(lookForEdit, 5000);
我明白为什么它不起作用,但我不知道如何修复它。
我该如何解决?
谢谢
嗯,马上,你设置 comp = res[0].date_created
然后,如果它们相等,你设置 comp = res[0].date_modified
但是下一次你调用 api,您正在用再次创建的日期覆盖 comp
。您应该使用 2 个单独的变量来跟踪信息。您应该为您尝试监控的值使用 2 个单独的更具描述性的名称。
使用全局变量存储最后一个值就足够了,只要先检查它是否被定义,如果没有就取date_create
值。
当然可以勾选date_modifed
只有在没有为未修改的项目定义的情况下才可以勾选
在这种情况下,您只需要检查是否定义了 date_modifed
值,如果是,则与 comp
(仍然是全局的)进行比较。然后如果有不同的存储新值。
PS:对不起,我没有写javascript遗嘱,所以不能写代码。我最终错误地回答了那个问题^^
这将是它的伪代码
global comp;
function(){
if(comp not defined)
comp = date_created
if(comp != date_modified)
//changed
comp = date_modified;
}
由于您的代码每 5 秒运行一次,我想您可以检查最后一次修改是否发生在不到 5 秒之前:
if(comp == res[0].date_modified){
console.log("the product hasn't been modified")
}
else if (new Date(Date.now().getTime() - 5000) < res[0].date_modified) {
console.log("the product hasn't been modified since the last check");
}
else{
console.log("The product has been edited")
comp = res[0].date_modified;
}