Javascript 将包含jquery的简单函数放入jquery执行队列
我有许多包含jquery的函数,它们应该按顺序执行。它们不用于创建效果,而是用于将某些元素放置在div内的计算位置。jquery队列上的所有在线资源都集中在转换或动画的创建上,因此很难找到一个简单的示例来说明如何按顺序执行多个简单函数 我有以下职能:Javascript 将包含jquery的简单函数放入jquery执行队列,javascript,jquery,multithreading,queue,Javascript,Jquery,Multithreading,Queue,我有许多包含jquery的函数,它们应该按顺序执行。它们不用于创建效果,而是用于将某些元素放置在div内的计算位置。jquery队列上的所有在线资源都集中在转换或动画的创建上,因此很难找到一个简单的示例来说明如何按顺序执行多个简单函数 我有以下职能: function bigItemRecalc(recalc, idBase, innerHeight, i) { if (recalc < 0) { $('#' + idBase + i).css('max-heigh
function bigItemRecalc(recalc, idBase, innerHeight, i) {
if (recalc < 0) {
$('#' + idBase + i).css('max-height', (innerHeight + (2 * recalc)));
recalc = 0;
}
return recalc;
}
function firstCheck(recalc, idBase, i) {
if (i == 1) {
$('#' + idBase + i).css('margin-top', recalc * -1);
}
}
function lastCheck(recalc, idBase, itemAmount, i) {
if (i == itemAmount) {
$('#' + idBase + i).css('margin-top', recalc);
}
}
function truncateItems(totalHeight, widgetHeight, idBase, i) {
if (totalHeight > (widgetHeight - 20)) {
$('#' + idBase + i).remove();
$('#' + idBase + "b" + i).remove();
}
}
这个问题解决了:
重新加载页面似乎没有触发.ready()函数。为什么不直接按顺序调用它们呢?(为什么标记为“多线程”?)我按顺序调用它们:recalc=bigItemRecalc(recalc,idBase,innerHeight,I);第一次检查(recalc、idBase、i);最后一张支票(信用证、idBase、项目金额、i);截断项(总高度、widgetHeight、idBase、i);但是Jquery不会等待前面的函数完成,这段代码没有异步性,所以一切都会按照您的要求进行。函数是按顺序执行的。当代码最初写在一个函数中时,某些代码被跳过。我添加了一个警报来检查一些变量,突然代码被完美地执行了。我认为警报造成的延迟导致代码正常执行。经过一些研究,我发现jQuery调用很可能没有在另一个调用之前完成。“如何按顺序执行许多简单函数”-没有什么特别的技巧:只需按顺序调用它们。尽管有一些jQuery函数可以启动异步进程,例如Ajax和动画方法,但是您所展示的代码都没有使用这些函数。对于您显示的内容,它将按顺序执行。
function styleWidget(itemAmount, widgetHeight, widgetWidth, idBase) {
var innerHeight;
var outerHeight;
var recalc;
var totalHeight = 0;
var totalWidth = 0;
for (var i = 1; i <= itemAmount; i++)
{
if (widgetHeight >= widgetWidth)
{
totalHeight += $('#'+idBase+i).height();
innerHeight = $('#' + idBase + i).height();
outerHeight = (widgetHeight/itemAmount);
recalc = ((outerHeight / 2) - (innerHeight / 2));
recalc = bigItemRecalc(recalc, idBase, innerHeight, i);
$('#' + idBase + i).css('padding-top', recalc);
firstCheck(recalc, idBase, i);
lastCheck(recalc, idBase, itemAmount, i);
truncateItems(totalHeight, widgetHeight, idBase, i);
}
else
{
innerHeight = $('#'+idBase+i).height();
outerHeight = widgetHeight;
recalc = ((outerHeight/2)-(innerHeight/2));
$('#'+idBase+i).css('padding-top',recalc);
totalWidth += $('#'+idBase+i).width();
if (totalWidth > (widgetWidth-20))
{
$('#' + idBase + i).remove();
$('#' + idBase + "b" + i).remove();
}
}
}
}
$(document).ready(styleWidget(3, 225, 169, 'id-871206010'));