同步: 順序交付的工做1234,必須按照1234的順序完成
複製代碼
document.getElementById('start').addEventListener('click', start, false);
function start() {
//響應事件,進行相應的操做
}
// jQuery用 '.on()'也是事件偵聽
$('#start').on('click', start);
複製代碼
// 比較常見的有ajax
$.ajax('http://baidu.com', {
success: function (res) {
// 這裏就是回調函數了
}
})
// 或者在頁面加載完畢後回調
$(function (){
// 這裏也是回調函數
})
複製代碼
a(function (a) {
b(a, function(b){
c(b, function(c) {
d(c, function(d){
console.log(d);
})
})
})
})
複製代碼
new Promise(
// 執行器 executor
function (resolve, reject) {
// 一段耗時很長的異步操做
resolve(); // 數據處理完成
reject(); // 數據處理出錯
}
).then(function A() {
// 成功,下一步
}, function B() {
// 失敗,作相應處理
})
複製代碼
console.log('xiao');
new Promise(resolve => {
setTimeout(()=>{
resolve('lin');
}, 2000);
}).then(value => {
console.log(value + 'ni hao');
})
複製代碼
console.log('xiao');
new Promise(resolve => {
setTimeout(()=>{
resolve('lin');
}, 2000);
}).then( value => {
console.log(value);
return new Promise(resolve => {
setTimeout(() => {
resolve('world');
}, 2000);
})
}).then(value => {
console.log(value + 'ni hao');
})
複製代碼
console.log('start');
let promise = new Promise(resolve => {
setTimeout(() => {
console.log('then promise fulfilled');
resolve('hello, world');
}, 2000)
});
setTimeout(() => {
promise.then(value => {
console.log(value);
});
}, 3000)
複製代碼
console.log('xiao');
new Promise(resolve => {
setTimeout(()=>{
resolve('lin');
}, 2000);
}).then( value => {
console.log(value);
console.log('everyone');
(function() {
return new Promise(resolve => {
setTimeout(() => {
console.log('Mr.xiaolin')
resolve('Mr.xiaolin');
}, 2000);
});
}());
return false;
}).then(value => {
console.log(value + 'ni hao');
})
複製代碼
console.log('start');
new Promise(resolve => {
console.log('Step 1')
setTimeout(()=>{
resolve(100);
}, 1000);
}).then( value => {
console.log(value);
return new Promise(resolve => {
console.log('Step 1-1')
setTimeout(() => {
resolve(110);
}, 1000);
})
}).then(value => {
console.log('Step 1-2');
return value;
}).then(value => {
console.log('Step 1-3');
return value;
})
.then( value => {
console.log(value);
})
複製代碼
有不正確請指正後續更新。。。java