自定义when语句不触发函数



我正在尝试制作一个when语句,但它没有按计划工作。基本上是一个函数,在尝试时调用另一个函数。在我进一步解释之前,首先是语法

when(function() {
    //code here
});

现在基本上。。。这样想。。我们有一个进度条。。我们还有一个自定义活动,如…

var pBarEvent = document.createEvent('Event');
pBarEvent.initEvent('pbardone', true, true);
document.addEventListener('pbardone', function() {
    //code here 
});
//if progress bar reaches 100 dispatchEvent
if (document.querySelector(".progress-bar").style.width === 100 + "%")
{
    document.dispatchEvent(pBarEvent);
}

现在,这段代码就是一个例子。例如,如果文档加载,并且它处于50%,则在添加其他事件(如keydown或click(之前,它不会触发。我不想做我想做的事……"当"进度条宽度等于100%时触发它。这基本上是需要发生的。到目前为止,when语句的代码如下(请记住,它不是最好看的语句。因为我通常不会这样做,但我想保持这种动态,谁知道后来想这样做的人可以看看这个问题(

当功能

function when(func)
{
    var nowActive = false;
    if (!typeof func === 'undefined')
    {
        func = new Function();
    }
    if (func)
    {
        nowActive = true;
        clearInterval(whenStatementTimer);
    }
    else 
    { 
        nowActive = false;
        var whenStatementTimer = setInterval(function() {
            switch(func)
            {
                case true: 
                {
                    nowActive = true;
                    when();
                    break;
                }
                case false: 
                {
                    nowActive = false;
                    when();
                    break;
                }
            }
        }, 1000);
    }
    if (nowActive === true)
    {
        func();
    }
}

现在,当我去尝试类似…的东西时,这不起作用。。。。

when(function() {
    SmartLeadJS.SmartLeadEvents.customEvents.progressBarFull(function() {
        alert("100%");
        SmartLeadJS.SmartLeadAds.LeadView.ChromeExtension.General.DynamicStyles.$.style("body", "background", "black");
    });
});

它不会触发。我需要帮助,可能在语句工作时得到帮助。我做错了什么?我能做些什么来修复它?不会抛出错误,但它永远不会启动。

根据答案进行编辑

尝试了功能

function when(currentValue)
{
    try
    {
        var o = {};
        o.currentValue = currentValue;
        o.do = function(func)
        {
            if (!typeof func === 'undefined')
            {
                func = new Function();
            }
            if (this.currentValue)
            {
                func();
            }
            else 
            {
                setTimeout(this.do(func), 100);
            }
        };
        return o;
    }
    catch(e)
    {
        console.log(e);
    }
}

用作

when(true).do(function() { 
    SmartLeadJS.SmartLeadEvents.customEvents.progressBarFull(function() { 
        alert("This divs going through changes!!");
        SmartLeadJS.SmartLeadAds.LeadView.ChromeExtension.General.DynamicStyles.$.style(".div", "background", "black");
    });
});

这不起作用。它从不着火。但如果我使用onclick监听器,它会触发

document.addEventListener("click", function() { 
    SmartLeadJS.SmartLeadEvents.customEvents.progressBarFull(function() { 
        alert("This divs going through changes!!");
        SmartLeadJS.SmartLeadAds.LeadView.ChromeExtension.General.DynamicStyles.$.style(".div", "background", "black");
    });
}, false);
function when(statement){
   o={};
   o.statement=statement;
   o.do=function(func){
      awhen(this.statement,func);
   };
   return o;
}
function awhen(statement,func){
   if(eval(statement)){
      func();
   }else{
      window.setTimeout(function(){awhen(statement,func);},100);
   }
}

用途:

when("true").do(function(){});

它现在工作:(。将条件置于"中很重要!

最新更新