Javascript 根据if语句的结果编辑if语句参数中变量的值

Javascript 根据if语句的结果编辑if语句参数中变量的值,javascript,node.js,for-loop,if-statement,woocommerce,Javascript,Node.js,For Loop,If Statement,Woocommerce,我想做的是每隔五秒钟联系WooCommerce,检查一个产品是否被编辑过。我可以通过请求res[0]来完成此操作。日期\u已修改。然后比较它的创建时间(res[0].date_created)和上次修改的时间 如果值相同,则产品从未被修改过。但是,如果产品被修改,我不想再比较产品创建和上次修改的时间 现在,我想将时间与上次使用“res[0].date\u modified”修改的时间进行比较,以便查看它是否在其他时间被修改 这就是我想到的: function lookForEdit(){ Wo

我想做的是每隔五秒钟联系WooCommerce,检查一个产品是否被编辑过。我可以通过请求res[0]来完成此操作。日期\u已修改。然后比较它的创建时间(res[0].date_created)和上次修改的时间

如果值相同,则产品从未被修改过。但是,如果产品被修改,我不想再比较产品创建和上次修改的时间

现在,我想将时间与上次使用“res[0].date\u 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);
我理解它为什么不工作,但我不知道如何修复它

我怎样才能修好它

谢谢



上周我也问了这个问题,我想我得到了一个可行的答案。我得到的答案是:

var comp;


function lookForEdit(){

WooCommerce.get('products/',  function(err, WooData, res) {
    res=JSON.parse(res)
    //console.log(res[0])
    
    if(comp == undefined){
        console.log("The product haven't been editet")
        comp = res[0].date_created;
    }
    if(comp != res[0].date_modified){
        comp = res[0].date_modified;
        console.log("The product have been edited")
        //editAPI(res[0])
        
    }

    


});
};
    
然后我意识到,当我把函数放在for循环中检查多个项目时,这是行不通的。如果检查索引0和索引1,则无法使用“comp”

再次感谢你




我想得到的

var comp;


function lookForEdit(){

WooCommerce.get('products/',  function(err, WooData, res) {
    res=JSON.parse(res)
    for(var i = 0; i < res.length; i++ ){

    if(comp == undefined){
        console.log("The product haven't been editet")
        comp = res[i].date_created;
    }
    if(comp != res[i].date_modified){
        comp = res[i].date_modified;
        console.log("The product have been edited")
        //editAPI(res[i])

    }
}



});
};
var-comp;
函数lookForEdit(){
get('products/',函数(err、WooData、res){
res=JSON.parse(res)
对于(变量i=0;i
您可以使用数组或对象在变量
comp
中存储多个值。在这种情况下,您可以使用
comp[0]=res[0].date\u modified
,而不是使用
comp[0]=res[0].date\u modified

完整示例:

var comp = {};

function lookForEdit(){

WooCommerce.get('products/',  function(err, WooData, res) {

    res=JSON.parse(res)

    for (var i = 0; i < res.length; ++i) {
        //console.log(res[i])

        if(comp[i] == undefined){
            console.log("The product haven't been editet")
            comp[i] = res[i].date_created;
        }
        if(comp[i] != res[i].date_modified){
            comp[i] = res[i].date_modified;
            console.log("The product have been edited")
            //editAPI(res[i])    
        }
    }
});
};
var comp={};
函数lookForEdit(){
get('products/',函数(err、WooData、res){
res=JSON.parse(res)
对于(变量i=0;i
您可以使用函数闭包来维护产品的状态,即它是新的还是旧的

var checkIfModified = function() {
    var isNew = true;
    return function() {
        var dateProp = isNew ? 'date_created' : 'date_modified';
        var time = test.shift();
        if( time !== product[dateProp]) {
            product.modified = time;
            isNew = false;
            console.log( 'modified')
        } else {
            console.log('it is same');
        }
    }
}

var lookForEdit = checkIfModified();
for( /*your for loop to check for edit*/ ) {
    lookForEdit();
} 

你的for循环在哪里?我没有包括在这里。我甚至还没有写过,但我知道这是行不通的。但是我可以把它添加到问题中,这样你就可以得到我想要的结果而不是
setInterval
,并将其放在
lookForEdit()
函数的末尾以循环endless我以前从未使用过用户setTimeout,这有什么关系?循环总是没完没了地运行着?谢谢非常感谢。很好用!公平地说,使用闭包比我的解决方案更惯用、更优雅。