function sendMessage(name, success, error) {
console.log(
`邓哥: -> ${name}:最近有谣言说我喜欢你,我要澄清一下,那不是谣言。`
)
console.log(`等待${name}回复......`);
setTimeout(() => {
if(Math.random() <= 0.5) {
success(`${name} -> 邓哥: 我是九,你是三,除了你还是你`);
} else {
error(`${name} -> 邓哥:你是个好人`);
}
}, 1000)
};
// sendMessage('李建国', function success(res) {
// console.log(res);
// }, function error(res) {
// console.log(res);
// sendMessage('下一位',
// function success(res) {
// console.log(res)
// },
// function error(res) {
// console.log('全部拒绝')
// })
// });
/**
* 利用promise
*/
function sendPromiseMessage(name) {
return new Promise((resolve, reject) => {
console.log(
`邓哥: -> ${name}:最近有谣言说我喜欢你,我要澄清一下,那不是谣言。`
)
console.log(`等待${name}回复......`);
setTimeout(() => {
if(Math.random() <= 0.5) {
resolve(`${name} -> 邓哥: 我是九,你是三,除了你还是你`);
} else {
reject(`${name} -> 邓哥:你是个好人`);
}
}, 1000)
})
};
// sendPromiseMessage('李建国').then(res => {
// console.log(res);
// }, err => {
// console.log(err);
// sendPromiseMessage('下一位').then(res => {
// console.log(res);
// }, err => {
// console.log('全部拒绝啦');
// });
// });
// 根据指定的图片路径,创建一个img图片
// 该函数需要返回一个Promise,当图片加载成功,任务成功,加载失败,任务失败
// 任务完成后,需要提供图片的dom元素, 失败提供失败的原因
// 提示:load事件,图片加载完成时触发,error事件加载失败时候触发
/**
* @returns {Promise}
*/
function createImg(src) {
return new Promise((resolve, reject) => {
const oImg = document.createElement('img');
oImg.setAttribute('src', src)
oImg.onload = function() {
resolve(oImg);
}
oImg.onerror = function(err) {
reject(err);
}
});
};
createImg('http://www.baidu.com/baidu.php?url=0s0000aPch_Smgbn9RPLrlxlN8yjmXj00gvLYK040QYLB2hzaRxsaMPvsT_ZbvIj41Nn1hlhcTI3lBvXRL4E8oQW3oK9d2raugIShLSq-VoOie4gvgJpmJ30vjDlPB3Srd7K4XmFKDzv7I6QzJlJ3CyKnyxEoxFEwOcozpWj8RXOfIyngM3V04zY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ckrHck0Z7xIWYsQW63g108njKxna3sn7tsQWDsg108njuxna34rNtsQWndg100mMPxTZFEuA-b5H00ThqGuhk9u1Ys0APv5fKGTdqWTADqn0KWTjYs0AN1IjYs0APzm1YzrHnknf&us=newvui&xst=mWdanHFKwH6krHNAPWnLfWR1f10sP1R3wWckrRfsPbD4n6715HDznWD1n1bznW01P1mvnHDLnWf1g1czPNtk0gTqz2l4O_JOk8mKTHv4O_JOk8mKIHY1nH6znHbznf7Y5HDvPjckPjR4Pj0KUgDqn0cs0BYKmv6quhPxTAnKUZRqn07WUWYYrj0znHcLnNt1r7qCmyqxTATKnHn4nHfLPWD4rf&word=&ck=7148.4.55.221.370.420.175.229&shh=www.baidu.com&sht=48021271_38_hao_pg&wd=').then(res => {
console.log(res);
}, err => {
console.log(err);
})
console