Reputation: 1205
var light = 'off';
$('#switch').toggle(function(){light='on'},function(){light='off'});
//in some case
$('#something').mouseout(function(){light = 'off'});
For example I clicked the switch, then light is on, at that time I mouseout something, the light is off, I want to turn up the light again, and click the switch again, but the switch think the light is still on and execute light = off again. I have to click it twice to turn up the light , how to solve this problem?
Upvotes: 1
Views: 266
Reputation: 4129
In such case I don't think toggle will be enough, since you have an outside influence on the state you toggle.
And so:
var light = 'off';
function toggleLight() {
if (light == "off")
light = "on";
else
light = "off";
}
$('#switch').click(toggleLight);
$('#something').mouseout(function() { light = "off"; });
Upvotes: 1