在调整大小时,我希望代码运行第一个 if 语句:“我认为这太小了”。在第二次调整大小时,我希望它运行第一个备用:“我认为这太大了”。它只运行一次,是不是因为变量调整只是局部的并且不会第二次停留?
var counter = 0;
function message() {
if (counter == 0) {
document.write("I think this is too small");
counter = counter + 1;
} else if (counter == 1) {
document.write("I think this is too big");
counter = counter + 1;
} else {
confirm("Third Time's a charm");
}
}
window.addEventListener('resize', message, false);
<p>Text</p>