自定义何时语句不激活函数

时间:2016-08-06 16:11:04

标签: javascript

我正在尝试制作一个if语句,但它没有按计划运行。基本上它是一个在尝试时调用另一个函数的函数。首先我在这里进一步解释是语法

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);

1 个答案:

答案 0 :(得分:0)

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(){});

它现在有效:)。将条件置于""!

非常重要