我需要在2/3秒后将此变量恢复为0,以便我可以再次按下它。
function Spawn() {
firebase.database().ref('Boolbtn').set({
Boolbtn: 1
});
};
这等待数据库首先设置值,而然后等待2/3秒。
function Spawn() {
firebase.database().ref('Boolbtn').set({
Boolbtn: 1
}, function() { // set value to zero after it is set in the db
setTimeout(function() {
firebase.database().ref('BoolBtn').set({
Boolbtn: 0
})
}, (2/3)*1000) // wait for 2/3 seconds
});
};
您可以通过以下方式进行操作:
function updateField(name, value) {
return firebase.database().ref( name ).set({ [name]: value });
}
function spawn() {
return updateField( 'BoolBtn', 1 ).then( _ => setTimeout( unspawn, 666 ) );
}
function unspawn() {
return updateField( 'BoolBtn', 0 );
}
// on click event
spawn();
您可能想使用setTimeout
:
function toggleButton(on) {
firebase.database().ref('Boolbtn').set({
Boolbtn: on ? 1 : 0
});
};
function Spawn() {
toggleButton(true);
setTimeout(() => {
toggleButton(false);
}, 1000 * 2 / 3)
}