Lets say I have some element with id="circle" and some button with id="button". All I need to do is:
- Circle is blinking by default;
- When user presses the button, circle stops blinking;
- When user presses the button once more, circle starts blinking;
- And so on.
I am trying to do this with the following code:
var blinking = true;
function flash(elementId) {
var bl = document.getElementById(elementId);
bl.style.visibility = bl.style.visibility == "hidden" ? "visible" : "hidden";
}
function buttonClick() {
if (blinking) {
clearInterval(flash('circle'));
} else {
setInterval(flash('circle'), 200);
}
}
setInterval(flash('circle'), 200);
<!DOCTYPE html>
<html>
<body>
<strong id="circle">●</strong>
<br>
<button type="button" id="leftButton" onclick="buttonClick()">toggle</button>
</body>
</html>
but it doesnt work in desirable way. If the solution's code in pure JS will be too large, you can write it with jquery, it doesnt really matter.