I wonder if it is possible in Javascript to have a click
event listener that every time I click changes my boolean from true to false back and forth. Meaning I click one it goes to false, I click again and it goes to true and so on in an infinite loop. I don't even know if it is possible but I tried this:
//This is my listener
circlePicker.click(function () {
booleanChecker(circlePickerSelector);
console.log(booleanChecker(circlePickerSelector));
});
//This function checks if the boolean is true or false
function booleanChecker(isThisTrue) {
// circlePickerSelector = !circlePickerSelector;
// return circlePickerSelector;
if (isThisTrue == false) {
isThisTrue = true;
console.log("I turned into true");
} else if (isThisTrue == true) {
isThisTrue = false;
console.log("I turned into false");
}
return isThisTrue;
}
I would like to know if this is possible. I get a feeling something is wrong in my syntax. Any suggestion is more than wele.
I wonder if it is possible in Javascript to have a click
event listener that every time I click changes my boolean from true to false back and forth. Meaning I click one it goes to false, I click again and it goes to true and so on in an infinite loop. I don't even know if it is possible but I tried this:
//This is my listener
circlePicker.click(function () {
booleanChecker(circlePickerSelector);
console.log(booleanChecker(circlePickerSelector));
});
//This function checks if the boolean is true or false
function booleanChecker(isThisTrue) {
// circlePickerSelector = !circlePickerSelector;
// return circlePickerSelector;
if (isThisTrue == false) {
isThisTrue = true;
console.log("I turned into true");
} else if (isThisTrue == true) {
isThisTrue = false;
console.log("I turned into false");
}
return isThisTrue;
}
I would like to know if this is possible. I get a feeling something is wrong in my syntax. Any suggestion is more than wele.
Share Improve this question edited Dec 14, 2021 at 20:53 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 14, 2014 at 18:26 mauricioSanchezmauricioSanchez 36610 silver badges23 bronze badges2 Answers
Reset to default 6You can toggle it by using the !
operator
circlePicker.click(function () {
circlePickerSelector = !circlePickerSelector;
console.log(circlePickerSelector);
});
If circlePickerSelector
was originally true
, then !circlePickerSelector
turns it to false
. You can then assign it to the same variable to do the reverse on next click.
Create a closure around a variable then return a function from inside that closure, for example,
function bool(initial) {
initial = !!initial;
return {
get current() {
return initial;
},
toggle: function () {
return initial = !initial;
}
};
}
var b = bool(true);
b.current; // true
b.toggle(); // false
b.current; // false
b.toggle(); // true
b.current; // true
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742348587a4427048.html
评论列表(0条)