code show as below:
function tips(){
//调用函数时,显示弹出层
$('.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...
}
淡淡烟草味2017-05-19 10:13:24
function tips(cb){
//调用函数时,显示弹出层
$('.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 => {
//调用函数时,显示弹出层
$('.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是个好东西,async/await更是
}
大家讲道理2017-05-19 10:13:24
function tips(confirmCallback, cancelCallback){
//调用函数时,显示弹出层
$('.mask').show();
//取消
$('.cancel').on('click',function(){
$('.mask').hide(); //隐藏弹出层
cancelCallback && cancelCallback();
return false;
});
//确定
$('.confirm').on('click',function(){
$('.mask').hide(); //隐藏弹出层
confirmCallback && confirmCallback();
return true;
});
}
ringa_lee2017-05-19 10:13:24
Add a flag. Flag can also be represented by numbers like 0 and 1.
function tips(){
var flag = false;
//调用函数时,显示弹出层
$('.mask').show();
//取消
$('.cancel').on('click',function(){
$('.mask').hide(); //隐藏弹出层
flag = false;
});
//确定
$('.confirm').on('click',function(){
$('.mask').hide(); //隐藏弹出层
flag = true;
});
return flag;
}
曾经蜡笔没有小新2017-05-19 10:13:24
I just learned a very stupid method
var tips_vla="";
function tips(){
//调用函数时,显示弹出层
$('.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("你点击了确认");
}else{
alert("你点击了取消");
}
}
滿天的星座2017-05-19 10:13:24
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