我想尝试打字稿和webpack我把一些代码放在IIFE中,然后运行两次。我增加一个计数器,它增加一次,但在浏览器中它输出两次。所以我得到了6行而不是3行。我认为这可能是webpack开发服务器的副作用或其他东西所以我在jsfiddle上模拟了相同的代码。它在那里按预期工作。
所以我使用webpack在本地构建了代码并在其上运行了http-server,它仍然输出两次。
let isInit = true;
let counter = 0;
const lcAppdata = {
matters: [{
mid: 100,
title: "Matter1",
dateOpened: "01/01/17",
decedent: "Elvis Presley",
personalRepresentative: "Alexis Texas",
attorneyAccountInfo: "John Holmes"
}, {
mid: 200,
title: "Matter2",
dateOpened: "01/02/16",
decedent: "Kurt Cobain",
personalRepresentative: "Kagney Linn Karter",
attorneyAccountInfo: "Simon Rex"
}, {
mid: 300,
title: "Matter3",
dateOpened: "01/03/15",
decedent: "Bob Marley",
personalRepresentative: "Nikki Benz",
attorneyAccountInfo: "Ron Jeremy"
}],
currentMatter: ''
};
(function init(){
for(let matter of lcAppdata.matters){
$('#tbodyMatterList').append(`<tr ><th scope="row" >${matter.title}</th>
<td bind>${matter.decedent}</td>
<td bind>${matter.dateOpened}</td>
<td bind>${matter.personalRepresentative}</td>
<td bind>${matter.attorneyAccountInfo}</td></tr>`);
console.log(matter.title);
}
counter++;
console.log(counter);
})();
控制台输出:
Matter1
Matter2
Matter3
1
Matter1
Matter2
Matter3
1
不知道从哪里开始,似乎只能获得准执行?计数器只增加一次?虽然展示了两次?