简体   繁体   English

在Javascript / Java函数中延迟代码执行

[英]Delay code execution within a function in Javascript / Java

I have an onclick javascript function (invoked from DOJO components through .jsp file) within which two UI components are loaded at the same time in js file (through an AJAX call to the Spring Controller (java file) and back to the JSP/Javascript file.) 我有一个onclick javascript函数(通过.jsp文件从DOJO组件调用),其中两个UI组件同时加载到js文件中(通过对Spring Controller(java文件)的AJAX调用并返回到JSP / Javascript)文件。)

The problem is that the second component loads before the first component and is not displaying the prescribed data . 问题是第二个组件在第一个组件之前加载,并且没有显示指定的数据。 I would like to know how to set a delay before calling the code in javascript to delay the second component in milliseconds. 我想知道如何设置延迟,然后再在javascript中调用代码以延迟第二个组件(以毫秒为单位)。

Note : 注意 :

  1. I used Thread.sleep(msecs) in my controller which seems to work fine and resolves my issue. 我在控制器中使用Thread.sleep(msecs)似乎可以正常工作并解决了我的问题。 But I dont want to use that knowing the risk it poses. 但是我不想用这种方式知道它带来的风险。 So request an alternative instead of this. 因此,要求替代它。

  2. Also I used setTimeOut() function but setTimeOut() requires a function as an argument. 我也使用了setTimeOut()函数,但是setTimeOut()需要一个函数作为参数。 I need to just delay the code within the function for a few milliseconds the first time only. 我只需要第一次将函数中的代码延迟几毫秒。 From second time onwards I dont want to delay the code being called. 从第二次起,我不想延迟被调用的代码。

Request ur valuable inputs. 要求您提供宝贵的意见。

JavaScript doesn't allow blocking threads, so the only option you can use is setTimeOut() function. JavaScript不允许阻塞线程,因此只能使用setTimeOut()函数。 Or you can implement mechanism when the first component notifies the second when it loads and at this moment the second component reloads itself. 或者,您可以在第一个组件加载时通知第二个组件并且此时第二个组件重新加载自身时实现机制。

Maybe your Problem can be solved by using http://dojotoolkit.org/reference-guide/1.8/dojo/domReady.html?highlight=domready to make sure tho dom is entirely loaded 也许可以通过使用http://dojotoolkit.org/reference-guide/1.8/dojo/domReady.html?highlight=domready来确保完全加载dom来解决您的问题

or by using dojo/deffered 或使用dojo / deffered

http://dojotoolkit.org/reference-guide/1.8/dojo/Deferred.html?highlight=domready http://dojotoolkit.org/reference-guide/1.8/dojo/Deferred.html?highlight=domready

regards 问候

Delaying the execution of code is not a good idea. 延迟执行代码不是一个好主意。 In development, the calls might be executed and returned in the order you want. 在开发中,调用可能会按照您想要的顺序执行并返回。 But in a production environment with a system under load, the timing of the server calls being executed may not be consistent. 但是在具有系统负载的生产环境中,执行服务器调用的时间可能不一致。

I am assuming that you are using dojo/xhr to make the ajax calls and your solution is to use a DeferredList . 我假设您正在使用dojo/xhr进行ajax调用,而您的解决方案是使用DeferredList

http://dojotoolkit.org/reference-guide/1.9/dojo/DeferredList.html http://dojotoolkit.org/reference-guide/1.9/dojo/DeferredList.html

var d1 = xhr(...);
    d2 = xhr(...);

var dl = new DeferredList([d1, d2]);

dl.then(function(result){
    // Execute the code that requires both calls to be completed.
});

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

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