<div id="c" onClick="func()"></div>
这将只在用户单击时执行funct()。但是我想在10秒后自动执行func()。有些人这样想:
<div id="c" After10Seconds="func()"></div>
编写一些逻辑。在页面加载中使用标志和timeout
:
var hasCalledFunc = false;
setTimeout(function() {
if (hasCalledFunc == false) func();
}, 10000);
function callFunc() {
if (hasCalledFunc == false) func();
}
function func() {
hasCalledFunc = true;
//..rest of func
}
和HTML:
<div id="c" onClick="callFunc()"></div>
或者,全部在func
var hasCalledFunc = false;
function func() {
if (hasCalledFunc) return;
else hasCalledFunc = true
//rest of func
}
setTimeout(func, 10000);