code show as below:
function tips(){
//調(diào)用函數(shù)時(shí),顯示彈出層
$('.mask').show();
//取消
$('.cancel').on('click',function(){
$('.mask').hide(); //隱藏彈出層
return false;
});
//確定
$('.confirm').on('click',function(){
$('.mask').hide(); //隱藏彈出層
return true;
});
}
I want to call it like this:
if( tips() ){
do_something_true...
}else{
do_something_false...
}
ringa_lee
function tips(cb){
//調(diào)用函數(shù)時(shí),顯示彈出層
$('.mask').show();
//取消
$('.cancel').on('click',function(){
$('.mask').hide(); //隱藏彈出層
cb(false);
});
//確定
$('.confirm').on('click',function(){
$('.mask').hide(); //隱藏彈出層
cb(true);
});
}
tips(function(ret){
if(ret) {
// blabla
} else {
// blabal
}
});
If you want to show off your skills, you can do it in ES7:
function tips(){
return new Promise(resolve => {
//調(diào)用函數(shù)時(shí),顯示彈出層
$('.mask').show();
//取消
$('.cancel').on('click',function(){
$('.mask').hide(); //隱藏彈出層
resolve(false);
});
//確定
$('.confirm').on('click',function(){
$('.mask').hide(); //隱藏彈出層
resolve(true);
});
});
}
async function test() {
if( await tips()) {
// do_something_when_true...
} else {
// do_something_when_false
}
// 是不是熟悉的味道?Promsie是個(gè)好東西,async/await更是
}
function tips(confirmCallback, cancelCallback){
//調(diào)用函數(shù)時(shí),顯示彈出層
$('.mask').show();
//取消
$('.cancel').on('click',function(){
$('.mask').hide(); //隱藏彈出層
cancelCallback && cancelCallback();
return false;
});
//確定
$('.confirm').on('click',function(){
$('.mask').hide(); //隱藏彈出層
confirmCallback && confirmCallback();
return true;
});
}
Add a flag. Flag can also be represented by numbers like 0 and 1.
function tips(){
var flag = false;
//調(diào)用函數(shù)時(shí),顯示彈出層
$('.mask').show();
//取消
$('.cancel').on('click',function(){
$('.mask').hide(); //隱藏彈出層
flag = false;
});
//確定
$('.confirm').on('click',function(){
$('.mask').hide(); //隱藏彈出層
flag = true;
});
return flag;
}
I just learned a very stupid method
var tips_vla="";
function tips(){
//調(diào)用函數(shù)時(shí),顯示彈出層
$('.mask').show();
//取消
$('.cancel').on('click',function(){
$('.mask').hide(); //隱藏彈出層
tips_vla=0;
al(tips_vla);
})
//確定
$('.confirm').on('click',function(){
$('.mask').hide(); //隱藏彈出層
tips_vla=1;
al(tips_vla);
});
}
tips();
function al(){
if(tips_vla){
alert("你點(diǎn)擊了確認(rèn)");
}else{
alert("你點(diǎn)擊了取消");
}
}
The laziest way is to add an exclamation mark, hee hee hee
if( !tips() ){
do_something_true...
}else{
do_something_false...
}
Of course, it is best to write a sentence in the tips() function, return true