Require.js(almond.js)定时关闭 [英] Require.js (almond.js) Timing Off

查看:121
本文介绍了Require.js(almond.js)定时关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Almond.js和Require.js优化器来制作一个独立文件,无需Require.js就可以使用它.我已经能够成功编译文件,并且所有代码都可以运行,但是有一个问题:代码以错误的顺序执行.

I'm trying to use Almond.js and the Require.js optimizer to make a standalone file that can be used without Require.js. I've been able to successfully compile the file, and all the code runs, but there's one problem: the code executes in the wrong order.

假设我的主文件(传递给优化器的include=选项中的文件)具有以下内容:

Let's say my main file (the one that's in the include= option passed to the optimizer) has the following:

console.log('outside');
require(['someFile'], function(someModule) {
    console.log('inside');
    window.foo = someModule.rightValue;
});

然后在我的HTML中有:

and then out in my HTML I have:

<script src="myCompiledFile.js"></script>
<script>
    console.log('out in the HTML');
</script>

由于我使用的是almond.js和一个已编译的文件(因此没有文件加载正在进行),所以我希望得到输出:

Since I'm using almond.js and a compiled file (and therefore there's no file loading going on) I would expect to get the output:

outside
inside
out in the HTML

但是,事实证明仍然存在一些异步性,因为我实际看到的是:

However, it turns out there's still some asynchronicity going on, because what I actually see is:

outside
out in the HTML
inside

,如果我尝试从HTML中检查window.foo,则该位置不存在.

and if I try to check window.foo from the HTML it's not there.

所以,我的问题是,如何使代码更像是普通/同步JS文件,该文件首先将其所有代码运行,然后再将其传递给下一个script块?我不能完全告诉我的用户将所有代码包装在window.setTimeout中."

So, my question is, how can I get the code to be more like a normal/synchronous JS file, which runs all its code first before passing things on to the next script block? I can't exactly tell my users "wrap all your code in a window.setTimeout".

推荐答案

默认情况下,Almond复制RequireJS require调用的计时语义.由于require在RequireJS中是异步的,因此在Almond中也是异步的.在来源中可以轻松观察到:杏仁使用setTimeout计划模块的执行,即使它可以立即执行它.这是有道理的,因为在 general 案例中,开发人员并不希望自己设计为异步工作的代码会突然因为他们使用Almond而变得同步.在每个项目中都无关紧要,但是在应该按一定顺序刷新UI(例如)的项目中,更改时间语义可能会破坏代码.

By default Almond replicates the timing semantics of RequireJS' require call. Since require is asynchronous in RequireJS, it is also asynchronous in Almond. This can be readily observed in the source: Almond uses a setTimeout to schedule the execution of a module even though it could execute it right away. It makes sense because in the general case developers don't expect that the code they've crafted to work asynchronously will suddenly become synchronous just because they are using Almond. It does not matter in every project but in a project where the UI (for instance) should be refreshed in a certain order, changing the timing semantics could break the code.

两个选项:

  • 作为setTimeout状态之前的注释,使用require('id')在Almond上 global 可以全局使用.这是因为一旦加载了包,就可以确保所有内容都已加载(因为Almond不会动态加载).

  • As the comment just before the setTimeout states, using require('id') works globally with Almond. This is because once your bundle is loaded, everything is guaranteed to have been loaded (since Almond does not load dynamically).

还有一种方法可以通过 extra来调用require论据.如果第四个参数为true,则调用将是同步的:

There's also a way to call require with extra arguments. If the fourth argument is true, the call is going to be synchronous:

require(['someFile'], function(someModule) {
    console.log('inside');
    window.foo = someModule.rightValue;
}, undefined, true);

这可以通过阅读杏仁代码来确定.我没有在forceSync上找到任何文档(这是相关参数的名称),但是一些错误报告

This can be ascertained by reading the code of Almond. There's no documentation I could find on forceSync (that's the name of the parameter in question) but some bug reports mention it and I've not seen any comment there that it is meant to be a private functionality.

这篇关于Require.js(almond.js)定时关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆