- 需要通过js实现异步回调的一个通用方法的处理
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>测试</title>
</head>
<script>
var i = 1;
function waitThing(callback){
var str = '';
var timerId = window.setInterval(function(){
var temp = document.getElementById('val').value;
if(temp != ''){
window.clearInterval(timerId);
str = temp;
callback(str);
}else{
i++;
document.getElementById('print').innerHTML = i;
}
}, 1000, this);
}
function callbackFun(str){
document.getElementById('print').innerHTML = '亲,一共执行了: '+i+' 次,您输入的是:'+str;
}
</script>
<body οnlοad="waitThing(callbackFun)">
<input name="" id="val" type="text" />
<div id="print"></div>
</body>
</html>