我可以使用哪些方法来重新排列 javascript 函数的执行顺序?

发布于 2024-08-12 06:22:32 字数 2202 浏览 3 评论 0原文

如何独立于包含顺序重新排列 javascript 函数执行顺序。

一个有帮助但不能完全回答我的问题的问题: 你可以有多个 $(document).ready(function() 部分吗? (答案是肯定的,它们按照添加到 jQuery.ready() 的顺序执行)。

我正在尝试为了方便通知,在我的 BBG ninjawars.net 我希望使用一个简单的系统,让服务器端 php 在页面登录时写出一个 javascript 函数调用 setLoggedIn() ,并写出一个对 clearLoggedIn 的调用() 当页面在注销时被查看时:

<head>
...
        <!-- All the global ninjawars javascript, defines the setLoggedIn(), clearLoggedIn(), and modifyPageLayout() functions and (currently) calls modifyPageLayout() within jQuery.ready() -->
 <script type="text/javascript" src="js/nw.js"></script>

 {if $logged_in} // Server-side check.
   <script type="text/javascript">
     <!--
      setLoggedIn();
     // -->
   </script>
 {else} // Server-side check found that the user was not logged in.
   <script type="text/javascript">
     <!--
      clearLoggedIn();
     // -->
   </script>
 {/if}
</head>

问题:

目前,顺序是:

  • 全局定义 setLoggedIn() 和clearLoggedIn()(在 nw.js 中)

  • 添加页面/信息修改代码,我们称之为modifyPageLayout(),它需要通知jQuery.ready()(在nw.ready()中)。添加

  • 调用setLoggedIn( ) 或clearLoggedIn() 内联在页面上的脚本标记中。

我不知道如何确保对 setLoggedIn() 或clearLoggedIn() 的调用发生在modifyPageLayout() 之前,modifyPageLayout() 当前包装在jQuery.ready() 块中,因此每当加载DOM 时就会运行。

  • 如果我将setLoggedIn()和clearLoggedIn()添加到jQuery.ready()块中,它们将按照添加的顺序执行,因此顺序将变为:modifyPageLayout(),然后setLoggedIn()setLoggedOut() 因此 jQuery.ready() 块中的所有内容都无法实现意图。

  • setLoggedIn() 和clearLoggedIn() 目前依赖于nw.js 定义的代码,所以我无法调用它们 在包含 nw.js 之前。

一种可能的解决方案:

我可以通过将其写为以下方式来延迟修改页面布局函数:

modifyFunction = modifyPageLayout; // Non-executed first-class function variable. (created in nw.js)

setLoggedIn(); // inline in the head of the page
modifyFunction(); // inline in the head of the page

任何人都有其他解决方案吗?

How to rearrange javascript function execution order independent of include order.

A question that assists, but doesn't completely answer my question: Can you have multiple $(document).ready(function() sections? (The answer is yes, and they execute in the order that they are added to jQuery.ready()).

I am trying to make my javascript logged-in/not logged-in aware, for convenient notification purposes, on my BBG ninjawars.net. I was hoping to use a simple system of having the server-side php write out a javascript function call setLoggedIn() when a page is logged in, and write out a call to clearLoggedIn() when a page is being viewed while logged out:

<head>
...
        <!-- All the global ninjawars javascript, defines the setLoggedIn(), clearLoggedIn(), and modifyPageLayout() functions and (currently) calls modifyPageLayout() within jQuery.ready() -->
 <script type="text/javascript" src="js/nw.js"></script>

 {if $logged_in} // Server-side check.
   <script type="text/javascript">
     <!--
      setLoggedIn();
     // -->
   </script>
 {else} // Server-side check found that the user was not logged in.
   <script type="text/javascript">
     <!--
      clearLoggedIn();
     // -->
   </script>
 {/if}
</head>

Problems:

Currently, the order is:

  • define setLoggedIn() and clearLoggedIn() globally (in nw.js)

  • Add page/information modification code, lets call it modifyPageLayout() that needs to be informed of logged-in/logged-out to jQuery.ready() (in nw.js)

  • Call setLoggedIn() or clearLoggedIn() inline in script tags on the page.

I don't know how to make sure that the calls to setLoggedIn() or clearLoggedIn() occur before modifyPageLayout(), which is currently wrapped in a jQuery.ready() block, and would thus run whenever the DOM loaded.

  • If I added setLoggedIn() and clearLoggedIn() to jQuery.ready() blocks, they would be executed in the order added, thus the order would become: modifyPageLayout(), then setLoggedIn() or setLoggedOut() so all in jQuery.ready() blocks doesn't accomplish the intent.

  • setLoggedIn() and clearLoggedIn() currently rely on code defined nw.js, so I can't call them
    before including nw.js.

One potential solution:

I could delay the modifyPageLayout function by writing it as:

modifyFunction = modifyPageLayout; // Non-executed first-class function variable. (created in nw.js)

setLoggedIn(); // inline in the head of the page
modifyFunction(); // inline in the head of the page

Anyone have other solutions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听你说爱我 2024-08-19 06:22:32

也许我错过了一些东西,如果我错过了,很抱歉。

但是对于 setLoggedIn() 和clearLoggedIn(),您难道不希望它们在有人单击登录或退出时发生吗?那么你可以在 document.ready 中添加一个事件处理程序来处理这个问题。

据推测,当某人登录或注销时发生某些事情的事实可以用于modifyPageLayout函数来决定显示什么以及如何显示。

不管怎样,我不确定这个问题是否比这更复杂

Maybe Im missing something, so sorry if I am.

But for setLoggedIn() and clearLoggedIn(), wouldnt you want them to happen any time someone clicks to log in or out? So then you could add an event handler inside the document.ready to deal with that.

Presumably, the fact that something happens when someone is logged in or out could be used to the modifyPageLayout function to decide what things show and how.

Anyway, Im not sure if the question is more complicated than that

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文