sharepoint online – Function not called in a conditional statement

I have a function that generates a table of contents. It works perfectly. However, this also works when the page is in edit mode and causes a lot of damage.

For that, I found the solution in the second part of the code below. The code seems to work well as evidenced by the outputs in the console log, corresponding to the current site ("true") or not ("false") in edit mode. However, when the site is NOT in edit mode, I think it's calling the GenerateTOC function, but this is not the case.

I have a very limited understanding of javascript, so I am a little lost here, especially if I think I can call other functions. Normally I called him by

_spBodyOnLoadFunctionNames.push (GenerateTOC & nbsp;)

but I guess it does not apply anymore …