我想要做的是每五秒钟联系一次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);
我理解为什么它不起作用,但我不知道如何解决它。
我该如何解决?
由于
答案 0 :(得分:0)
嗯,刚开始,你正在设置comp = res[0].date_created
然后,如果它们相等,你设置comp = res[0].date_modified
,但是当你下次调用api时,你正在覆盖{{1}再次创建日期。您应该使用2个单独的变量来跟踪信息。您应该为要监视的值使用2个单独的更具描述性的名称。
答案 1 :(得分:0)
使用全局变量存储最后一个值就足够了,只需检查是否先定义它,如果不是date_create
值。
当然,只有在未为未修改的项目定义date_modifed
时,才能检查date_modifed
。
在这种情况下,您只需要检查是否定义了comp
值,如果是,则与global comp;
function(){
if(comp not defined)
comp = date_created
if(comp != date_modified)
//changed
comp = date_modified;
}
(仍为全局)进行比较。然后,如果有不同的存储新值。
PS:抱歉,由于我没有在遗嘱中写javascript,因此我无法编写代码。我错误地结束了那个问题^^
这将是它的伪代码
Map<Integer, LinkedList<Integer>>
答案 2 :(得分:0)
由于您的代码每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;
}