如何等待,直到要求在dojo完成

编程入门 行业动态 更新时间:2024-10-24 23:30:44
本文介绍了如何等待,直到要求在dojo完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在为其他开发人员提供基础架构,而我正在使用Dojo。

在我的Init函数中,我使用'require'方法(当然)其中一个参数是所有模块加载后的回调函数。

问题是客户端不想使用回调。他想要打电话给我,并使用我(使我的Init方法同步) - 并且在我们确定完成加载我们的模块后,给他代码。

我的代码

< script src = .... / dojo.js>< / script> 函数Init() { var loaded = false; require([... myFiles ...], function() { loaded = true; }); //无论做什么或其他一些方法,直到require操作完成 while(!loaded)// :) }

我的客户端

Init (); myFiles.DoSomething();

有可能吗? 要使同步或做别的事情等待,直到结束? 只有在require方法完成后才能从init方法返回?

解决方案

您可以调用下一个函数在需求响应中,或使用延迟对象。

函数Init() { var d = new dojo.Deferred(); require([... myFiles ...], function() { d.resolve(true); } ); return d; }

其他文件

Init()。then(myFiles.DoSomething);

I'm providing an infrastructure for other developers, and i'm using Dojo for it.

In my Init function i'm using the 'require' method (of course) which one of the parameters are the callback function when all the modules have been loaded.

The problem is that the client don't want to use callbacks. He wants to call me and line after to use me (to make my Init method synchronized) - and give him the code back after we for sure finished loading our modules.

My Code

<script src=..../dojo.js></script> function Init() { var loaded = false; require(["...myFiles..."], function() { loaded = true; }); // Whatever to do here or some other way to hold till the require operation finished while (!loaded) // :) }

My Client side

Init(); myFiles.DoSomething();

Is it possible to do it ? To make require synchronized or do something else that waits till it's over ? To return from the init method only when the require method finished ?

解决方案

You can either call your next function from within the require response, or use a deferred object.

function Init() { var d = new dojo.Deferred(); require(["...myFiles..."], function() { d.resolve(true); }); return d; }

Other file

Init().then(myFiles.DoSomething);

更多推荐

如何等待,直到要求在dojo完成

本文发布于:2023-11-09 10:03:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572085.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:dojo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!