简体   繁体   English

Javascript,暂停/恢复 window.load 事件

[英]Javascript, pause/resume window.load event

I have an unusual problem that is beyond my client side skill set.我有一个不寻常的问题,超出了我的客户端技能范围。 I have an html page with two consecutive script tags in the body.我有一个 html 页面,正文中有两个连续的script标签。 Both script tags fire an ajax request to retrieve content and then put it in the DOM.两个script标签都会触发 ajax 请求来检索内容,然后将其放入 DOM。 I control the first script tag, and I don't have any control or knowledge about the second script tag.我控制第一个script标签,而我对第二个script标签没有任何控制或知识。 I just know it makes an ajax call and put stuff in the DOM.我只知道它会调用 ajax 并将内容放入 DOM 中。 I also know that my tag is first.我也知道我的标签是第一位的。

I want my script tag to fire immediately and pause/stop the second tag.我希望我的脚本标签立即触发并暂停/停止第二个标签。 Then, contingent on what my ajax returns, I may choose to execute the second script tag.然后,根据我的 ajax 返回的内容,我可以选择执行第二个脚本标记。 How do I do this?我该怎么做呢?

I wrote a fiddle demonstrating my problem.我写了一个小提琴来演示我的问题。 I substituted settimeout 's for ajax calls and Math.random for my decision.我用settimeout代替 ajax 调用,用Math.random代替我的决定。 Note that the settimeout duration for the first one (my one) is very long, 3 seconds, and the duration for the second one (the one I don't control is very short, .5 seconds. What's happening now is that they are both running immediately. The second message comes in at .5 seconds, and the message from the first comes in at 3 seconds 50% of the time.请注意,第一个(我的)的settimeout持续时间很长,3 秒,而第二个(我无法控制的那个很短,0.5 秒)的持续时间。现在发生的事情是它们是都立即运行。第二条消息在 0.5 秒后出现,第一条消息在 50% 的时间里在 3 秒后出现。

I want the first script to run, wait 3 seconds, make a decision and either print the first message immediately, or call the second script which would wait an additional .5 seconds to print the second message.我希望第一个脚本运行,等待 3 秒,做出决定并立即打印第一条消息,或者调用第二个脚本,该脚本将额外等待 0.5 秒以打印第二条消息。 I should never see both messages at the same time, and I should never see the second message before 3.5 seconds.我永远不应该同时看到两条消息,也不应该在 3.5 秒之前看到第二条消息。

Again, I don't know any knowledge over the contents of script two.同样,我对脚本二的内容一无所知。 You could easily hide id= ext_div in this case, but I don't know what that id is.在这种情况下,您可以轻松隐藏 id= ext_div ,但我不知道该 ID 是什么。 That is not the answer I want.那不是我想要的答案。 I was thinking about grabbing the event on the first script and doing something to that, stopPropagation or something like that at the TODO1 comment.我正在考虑在第一个脚本上获取事件并在TODO1评论中对此做一些事情, stopPropagation或类似的事情。 But that doesn't work.但这不起作用。 Event if it did, I would need a resumePropgation function which I don't event think exists.如果有的话,我需要一个我认为不存在的resumePropgation函数。

I want my script tag to fire immediately and pause/stop the second tag.我希望我的脚本标签立即触发并暂停/停止第二个标签。 Then, contingent on what my ajax returns, I may choose to execute the second script tag.然后,根据我的 ajax 返回的内容,我可以选择执行第二个脚本标记。 How do I do this?我该怎么做呢?

The only way to make that happen is to make your ajax call synchronous, eg, pass false as the third argument to XMLHttpRequest#open (or whatever the equivalent is in the library you're using, if you're using one).实现这一目标的唯一方法是使您的 ajax 调用同步,例如,将false作为第三个参数传递给XMLHttpRequest#open (或者您正在使用的库中的任何等效项,如果您正在使用)。 There are several reasons why this is a bad idea , but if you absolutely, positively need your ajax call to complete before the next script tag is executed, that's the only way to do it.这是一个坏主意的原因多种,但如果您绝对需要在执行下一个script标记之前完成 ajax 调用,那么这是唯一的方法。 Of course, it means that any DOM elements defined below your script tag in the HTML document won't exist yet.当然,这意味着在 HTML 文档中的script标签下定义的任何 DOM 元素都不存在。

It feels like you want some sort of dependency management system for your front-end scripts.感觉就像您需要某种前端脚本的依赖管理系统。

Try out RequireJS .试试RequireJS You could load in your script, run your ajax request, and depending on your logic, decide whether or not to load the second external script.您可以加载脚本,运行 ajax 请求,并根据您的逻辑决定是否加载第二个外部脚本。

This is assuming you can control the markup on the page of course.这是假设您当然可以控制页面上的标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM