HTML代码:
<button id="button1">点击弹出结果1</button>
<button id="button2">点击弹出结果2</button>
<button id="button3">点击弹出结果3</button>
JS代码:
var someVar = 'hello';
document.getElementById("button1").onclick = function() {
(function() { alert('hello'); })(); //弹出 'hello'
};
setTimeout(function() {
document.getElementById("button2").onclick = function() {
alert(someVar);
};
}, 1000);
setTimeout((function(someVar) {
return function() {
document.getElementById("button3").onclick = function() {
alert(someVar);
};
}
})(someVar), 1000);
var someVar = 'goodbye';