jQuery $.when() with variable arguments

2019-01-14 16:17发布

I want to send [1, n) AJAX-requests to the server and after all have returned a result a modal dialog should be closed. $.when(a(), b(), c()) would be perfect, but I don't know how to pass the variable count of functions to $.when as parameter. Any ideas how to solve this problem?

1条回答
一纸荒年 Trace。
2楼-- · 2019-01-14 16:35

Call the functions and add their return values to an array. Then call $.when passing the array as argument like so:

$.when.apply($, array)

See Function.prototype.apply [MDN] for more information and my answer here for an extended example.

查看更多
登录 后发表回答