Javascript 在另一个函数中调用一个函数,然后停止它

Javascript 在另一个函数中调用一个函数,然后停止它,javascript,jquery,html,css,function,Javascript,Jquery,Html,Css,Function,我有一个文件输入,在其上调用了两个函数,f和colorit。如果f中不满足指定的条件,我想停止函数colorit。但是,一旦选择文件,两个功能都会启动 <body> <div class="dClass"> <input type="file" class="iClass" /> </div> <script> function f(){ if () {

我有一个文件输入,在其上调用了两个函数,f和colorit。如果f中不满足指定的条件,我想停止函数colorit。但是,一旦选择文件,两个功能都会启动

<body>
    <div class="dClass">
        <input type="file" class="iClass" />
    </div>

    <script> 
        function f(){
            if () { 
                // if image selected does not meet requirements
                // how to stop colorit(); function here ??
                return false;
            }
            else {
               alert("done");
            }
        }

        $('.iClass').change(function() { 
           f(); // calling function f
        });

        $(function() {
           $('.dClass').colorit();
           //note: colorit() function also comes to action as soon as a file is selected through .iClass
        });
    </script>
</body>
删除—

$(function() {
   $('.dClass').colorit();
}
如果你只想让它在f成功的情况下工作

定义f-like-

这样,您就可以在sicceeds的情况下使用对象启动colorit

并称f为-

另一种方法是:

function f(obj){
    if () { // if image selected does not meet requirements
       // how to stop colorit(); function here ??
       return false;
    }
    else {
       obj.data('fdone', 1);
       alert("done");
    }
}

及-

<input type="file" class="iClass" data-fdone='0' />

您可以通过在函数中使用它来强制停止函数执行,而不是重新命名,但这是一种方法

throw new Error('This is not an error. This is just to abort javascript');

这是一个改变div背景的函数,我想要的是如果图像不符合条件,不要实现为背景…并且你的colorit函数运行onload…这是一个循环还是什么?0_oYeah似乎值得一试,shud你收到警报了吗?Colorit是一个来自jquery插件的巨大函数,这会影响它的功能吗?这个throw有什么新功能?throw语句抛出一个用户定义的异常。当前函数的执行将在不会执行throw之后停止语句,并且控制将传递给调用堆栈中的第一个catch块。如果调用函数之间不存在catch块,则程序将终止。能否将我们设为JSFIDLE?first FIDLE.。在这个例子中,无论f的条件是否满足,它都会显示背景图像。副手。在这个例子中,f验证if条件,但它不起作用。在jsFiddle中,你使用jquery,但没有选择任何库,它起作用了吗?或者你只是再次将代码粘贴到那里?还有一个问题,它是一个插件吗?还是你做的?你的意图是什么?如果图像通过某些条件,是否上载图像并将其设置为背景?
$('.iClass').change(function() { 
   f($('.dClass')); // calling function f
   if($(this).data('fdone') == 1) {
      $('.dClass').colorit();
   }
});
<input type="file" class="iClass" data-fdone='0' />
throw new Error('This is not an error. This is just to abort javascript');