简体   繁体   English

Javascript-停止函数执行

[英]Javascript - Stop function from executing with function

I want to insert my debugger function inside another JS function and halt the execution. 我想将debugger函数插入另一个JS函数中并停止执行。

I know return false/true does the job, but I want my debugger function to do that automatically. 我知道return false / true可以完成这项工作,但是我希望我的调试器函数自动执行此操作。

Example: 例:

<script type="javascript">
    function validateFirstName () {
        //validating first name field
        var fn = $('#firstname').val();
        if(fn == "") {
            $("#errorMsg").html("Please insert first name");
            $("#firstname").focus();
            return false;
        }

        debugger(); //I want to stop everything here

        // if validation passes, redirect browser:
        window.location = 'nextpage.html';
    }

    function debugger () {
        console.log("Some custom message here");
        return false;
    }
</script>

You'll notice I put my debugger function inside the validateFirstName() function. 您会注意到我将debugger函数放在validateFirstName()函数中。

I assumed that return false in my debugger() function will stop the validateFirstName() from executing. 我假设在debugger()函数中return false将阻止validateFirstName()的执行。 But it doesn't. 但事实并非如此。

Without adding return false inside the validateFirstName() function, how can I use my debugger() function to stop all execution? 没有在validateFirstName()函数内添加return false,如何使用我的debugger()函数停止所有执行?

replace 更换

debugger(); //I want to stop everything here

with

return debugger(); //I want to stop everything here

the above example will always stop on true or false. 上面的示例将始终停留在true或false上。

This will continue to the window.location if it's true and stop if it's false. 如果为true,它将继续到window.location,如果为false,则将停止。

if(!debugger())
  return;

in your case it seems to be a function inside of a function so you might as well use 在您的情况下,它似乎是函数内部的一个函数,因此您不妨使用

if(!debugger())
  return false;


Seems what you really want to do is set a breakpoint on the executing code. 似乎您真正想做的是在执行代码上设置一个断点。
In Chrome Browser press Ctrl+Shift+I 在Chrome浏览器中,按Ctrl+Shift+I
Then Go to Sources Tab 然后转到“ Sources选项卡
Click the Arrow pointing right (looks like a play button) on top of the counting line numbers to see list of websites 单击计数行号顶部的向右箭头(看起来像一个播放按钮),以查看网站列表
Find your website click on the folder 找到您的网站,点击文件夹
Find whatever script that you want 查找所需的任何脚本
Now click anywhere in the code to close the side bar 现在单击代码中的任意位置以关闭侧栏
Now finally click on any number on the side thats counting down the lines 现在,最后单击要倒数的那边的任何数字
That will set a breakpoint which means it will stop on that code if you make the code go there by doing something on your website, or forcing the code to run using the Console tab or simply in your address bar typing javascript: function_to_call(); 这将设置一个breakpoint ,这意味着如果您通过在网站上执行某些操作来使代码继续运行,或者使用“ Console选项卡或仅在地址栏中键入javascript: function_to_call();来强制运行代码,则它将在该代码上停止javascript: function_to_call();

You could throw from debugger () like this. 您可以像这样从debugger ()抛出。

function debugger () {
    console.log('Some custom message here');
    throw 'Debugging Code';
}

Although this will do what you want it to, I don't recommend it. 尽管这可以满足您的要求,但我不建议这样做。 Basically what's happening is you are throwing an error which isn't being caught in your code (the browser will catch it, but that is probably not as clean). 基本上发生的事情是您抛出了一个代码中未捕获的错误(浏览器会捕获该错误,但这可能不是很干净)。

You could throw an error: 您可能会抛出错误:

function validateFirstName () {
        //validating first name field
    var fn = $('#firstname').val();
    if(fn==""){
      $("#errorMsg").html("Please insert first name");
          $("#firstname").focus();
      return false;
        }

debugger(); //I want to stop everything here

// if validation passes, redirect browser:
window.location='nextpage.html';

}


function debugger () {
    throw new Error("Some custom message here");
}

try{
    validateFirstName();
}catch(e){
   console.log(e);
}

If you are using a modern browser like Chrome, why not just use debugger instead? 如果您使用的是现代浏览器(例如Chrome),为什么不只使用debugger呢?

that will trigger the debugger in your developer tools. 这将触发开发人员工具中的调试器。

like this: 像这样:

debugger; //I want to stop everything here

notice the missing () 注意丢失的()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM