我的代码在' $.当block没有触发jQuery时
My code inside `$.when` block is not firing with jQuery
我试图在jQuery中使用$.when(some function).then(some function)
来强制回调函数等待,直到$.when()
中的代码首先完成。
这是我的功能。
function activateFirstTab(callback) {
console.log('started activateFirstTab');
console.log('Total groups found: '+ $('.functionParameterGroup').length);
$.when(function () {
$('.functionParameterGroup').each(function () {
//Activate the first tab "which is the default"
var first = $(this).find('ul > li:first');
first.addClass('active');
var contentId = first.data('contentid');
$(contentId).addClass('in active');
console.log('finished when logic in activateFirstTab');
});
}).then(function () {
// Make sure the callback is a function
if (typeof callback === "function") {
// Call it, since we have confirmed it is callable
callback();
}
});
console.log('finished activateFirstTab');
}
在控制台中,我得到以下消息
started activateFirstTab
Total groups found: 2
finished activateFirstTab
但是我从来没有从$.when()
我期望输出是这样的
started activateFirstTab
Total groups found: 2
finished when logic in activateFirstTab
finished when logic in activateFirstTab
finished activateFirstTab
为什么$.when()
里面的代码没有被执行?
我如何让它执行它?我犯了什么错误?
你不调用$.when()
内的函数,也注意传递给$.when()
的匿名函数不返回承诺;尽管任何类型的参数都可以传递给$.when()
function fn(callback) {
$.when(function dfd(callback) {
$.each([1, 2, 3], function(i, value) {
console.log(value)
});
return "dfd complete"
}() /* invoke `dfd` function */ )
.then(function(data) {
// Make sure the callback is a function
if (typeof callback === "function") {
// Call it, since we have confirmed it is callable
callback(data);
}
})
}
fn(function(args) {
alert(args)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
相关文章:
- 无法从 jQuery RSS Feed 中的 localStorage 动态替换类
- 如何使用jQuery自动打开页面上的所有链接
- 如何使用jquery在填充自动完成的值后使文本框只读
- jQuery:循环一个具有不同超时值的循环
- jQuery show/fadeIn不将display:block应用于隐藏的父元素内的子元素-仅限FireFox
- LTR to RTL jQuery Collapsibles block
- CSS: jQuery:将右边距应用于 li 元素,除了每个第 4 个子元素(仅适用于 display:block 的元
- 在 @ Block Razor 中使用 jQuery 变量
- Jquery read xml block
- 使用block/none显示/隐藏jquery
- jQuery .then() 和 success block 不能一起工作
- 如何让jQuery UI标签做'display: flex'代替'display: block&
- Jquery toggle设置属性为display: table而不是block
- 显示-inline-block与z-index和jquery
- 我的代码在' $.当block没有触发jQuery时
- jQuery toggle table元素:use "display: block"而不是"
- jQuery UI.在Block UI加载后执行一个函数
- Block jquery移动脚本和元视角为桌面用户
- 当标签'display:block'时,Javascript失去了对第一个无效字段(jQuery Val
- 设置jQuery .offset();在“显示:没有;“显示与"display:block;"不同的