How to autofocus in CodeMirror.js

I am using CodeMirror v6 in javascript. I found https://discuss.codemirror.net/t/how-to-auto-focus-when-the-editor-loaded/5891/ and wrote this:

function autofocus(editor, info) {
    console.log(1);
    editor.focus();
}

editor = CodeMirror(document.getElementById("code_area"), {
    lineNumbers: true,
    ...
});
editor.on("blur", autofocus);

It worked at first, but when I clicked for the second time, the function didn’t work. I don’t know why.

I’m not sure what went wrong with that, but refocusing the editor when the user is trying to move focus elsewhere doesn’t seem like a very good idea to begin with.

OK. Maybe I should refocus when a button is pressed.

But I still wonder why it didn’t work.