简体   繁体   中英

How do I debug Javascript Function in vscode?

Macbook, OSX 10.9 VsCode - Enterprise 2019

// Issue Description

I have a very simple JS function


function countup(n) {
    if (n < 1) {
      return [];
    } else {
      const countArray = countup(n - 1);
      //debugger;
      countArray.push(n);
      return countArray;
    }
  }
  console.log(countup(5));

What I need help with?

Run this with a debugger on vscode by adding breakpoints in between. When I try to use the launch.json config


{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "chrome",
            "request": "launch",
            "name": "Launch Chrome",
            "file": "${workspaceRoot}/Test.js"
        }
    ]
} 

This basically renders the JS file in Chrome but does not execute the actual function. I have tried the same with a local server + chrome debugger extension in vs code.

How do I get this to work?

to run the JS code, you need to open a new HTML file and run the code in the HTML. if it is just a basic code you can use the Script tag <script> and call the function in the body tag using the onload Event. after you do that you can use the chrome debugger (press F12)

here an example

<body onload="Myfunction()">
    <script>
        function Myfunction() {
        console.log('hello world');
        }
    </script>
</body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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