下面是Google推荐的代码。这些代码应被放置在</body>标签前(接近HTML文件底部)。另外,我将外部JS文件名突出显示。
JavaScript
<script type="text/javascript">
function downloadJSAtOnload() {
var element = document.createElement("script")
element.src = "defer.js"
document.body.appendChild(element)
}
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false)
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload)
else window.onload = downloadJSAtOnload
</script>
1.[self performSelector:@selector(函数名) withObject:nil afterDelay:5.0f]条件:在uiviewController的主线程中
2.[NSTimerscheduledTimerWithTimeInterval:5.0f target:self selector:@selector(函数名) userInfo:nil repeats:NO]
在具体函数中,执行代码,执行完毕以后调用NSTimer invalidate方法来销毁timer
3.[NSThread sleepForTimeInterval:5.0f]
[要延迟执行的方法]
条件:主线程或者子线程都可