如何在 client/user 进入站点后立即激活功能

How to activate a function as soon as the client/user enters the site

我希望能够在进入我的网站后立即呼叫 myFunction。我该怎么做?

    function myFunction() {
        myVar = setTimeout(alertFunc, 3000);
    }

    function alertFunc() {
        alert("Hello!");
    }) 

只需在末尾添加对 myFunction 的调用 - 我不相信你实际上是在调用你的函数,只是定义它们:

function myFunction() {
  myVar = setTimeout(alertFunc, 3000);
}

function alertFunc() {
  alert("Hello!");
}

myFunction();

此外,您在 alertFunc() 定义的末尾有一个额外的括号 ) 的语法错误。

function alertFunc() {
    alert("Hello!");
}

window.onload = function() {
 myVar = setTimeout(alertFunc, 3000);
};

通过填充正文的 onload 字段加载页面后立即执行 JavaScript。

下面是一些示例代码:

<html>
    <body onload="myFunction()">
        <script>
        function myFunction() {
            //here put your immediate action e.g.
            alert("Page is fully loaded");
        }
        </script>
    </body>
</html>