我在我的项目的情况,我必须有两个for循环一个后对方:两个for循环(一个接一个)是否以第二个循环只在第一个循环完成时才同步执行的方式同步执行?
for (var i = this.props.activeChannel.channelsArr.length - 1; i > -1; i--) {
$("#" + this.props.activeChannel.channelsArr[i].channelName.replace(/ /g, '-').toLowerCase()).find("img").attr("src", "http://localhost:3003/images/menu-icons/" + $("#" + this.props.activeChannel.channelsArr[i].channelName.replace(/ /g, '-').toLowerCase()).find("img").attr("src").getTheImgNameFromUrl().replace("-grey", "") + ".png");
$("#" + this.props.activeChannel.channelsArr[i].channelName.replace(/ /g, '-').toLowerCase()).addClass("dummyActive");
$("#" + this.props.activeChannel.channelsArr[i].channelName.replace(/ /g, '-').toLowerCase()).removeAttr("style").css({
"background-color": "#96A2B3"
});
$("#" + this.props.activeChannel.channelsArr[i].channelName.replace(/ /g, '-').toLowerCase()).removeClass("dummyNotActive");
$("#" + this.props.activeChannel.channelsArr[i].channelName.replace(/ /g, '-').toLowerCase() + " a");
$("#" + this.props.activeChannel.channelsArr[i].channelName.replace(/ /g, '-').toLowerCase() + " div").removeClass().addClass("channel-activation-btn").removeAttr('style');
//$("#" + this.props.activeChannel.channelsArr[i].channelName.replace(/ /g, '-').toLowerCase()+" a").css({"display":"block"});
$("#" + this.props.activeChannel.channelsArr[i].channelName.replace(/ /g, '-').toLowerCase()).insertBefore("#" + $("#content li:nth-child(1)").attr("id"));
}
for (var i = this.props.activeChannel.channelsArr2.length - 1; i > -1; i--) {
$("#" + this.props.activeChannel.channelsArr[i].channelName.replace(/ /g, '-').toLowerCase()).removeClass("dummyActive").addClass("dummyNotActive");
$("#" + this.props.activeChannel.channelsArr[i].channelName.replace(/ /g, '-').toLowerCase()).removeAttr("style").css({
"background-color": "#E5E8EC"
});
// $("#" + this.props.activeChannel.channelsArr[i].channelName.replace(/ /g, '-').toLowerCase()+" div").removeClass("channel-activation-btn");
// $("#" + this.props.activeChannel.channelsArr[i].channelName.replace(/ /g, '-').toLowerCase()+" div").removeClass().removeAttr('style').css({"width":"100%","height":"100%"});
//$("#" + this.props.activeChannel.channelsArr[i].channelName.replace(/ /g, '-').toLowerCase()).insertBefore("#" + $("#inactiveContainer li:nth-child(1)").attr("id"));
$("#" + this.props.activeChannel.channelsArr[i].channelName.replace(/ /g, '-').toLowerCase()).appendTo("#inactiveContainer");
}
因此,大家可以看到有两个for循环和内部的每个有其操纵几行CSS和将元素插入不同的位置,并且不存在Asynch调用(Ajax)。我需要知道的是上面的for循环将按顺序执行?对我来说非常重要的是,第一个循环完成其中的所有行,然后第二个for循环开始执行。所以我可以说100%确定第一个循环完成后第二个循环会执行吗?
为什么会认为它们不是同步执行的? –
循环被同步执行。但是,页面渲染将在所有JS执行完毕后完成,因此当第二个循环将反转更改时,您无法看到第一个循环中对类名称所做的更改。 – Teemu
@ScottHunter我搜索了它,我注意到for循环是同步的,但我只是想确保我的理解,因为我有很多异步执行的问题,我需要确保 –