/    Sign up×
Community /Pin to ProfileBookmark

stop a function with a timeout set

I am using this simple script to cause text to blink. My only problem is that I cannot get it to stop with a button.
Here is the code – might make more sense looking at it.

<script language=”javascript”>
function blinkText1() {
document.getElementsByTagName(‘div’).item(0).style.visibility = “hidden”; setTimeout(“blinkText2()”, 1000)
}

function blinkText2() {
document.getElementsByTagName(‘div’).item(0).style.visibility = “visible”; setTimeout(“blinkText1()”, 1000)
}

function stopBlink() {
//what can I put here to cause the “loop” to stop?
}
</script>
</head>

<body>
<!– Item 0 –>
<div style=”visibility: visible”>
<center><h1>HEADING</h1></center>
</div>
<center><input type=”button” value=”Blink Text” onclick=”blinkText1()”></center><p>
<center><input type=”button” value=”Stop” onclick=”stopBlink()”></center>
</body>

I appreciate any help.
Thank you.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisApr 25.2004 — You could use a variable to gate the blink. You put an "if" statement in one of the blink functions that tests the variable. Then the stop button just sets the variable.

You could greatly simplify your code by using setInterval().
<i>
</i>var _blink = false;

function makeBlink() {
it = document.getElementById("d1").style;
it.visibility = ((it.visibility == "visible") ? "hidden" : "visible");
}

function blinkIt() {
_blink = setInterval("makeBlink()", 1000);
}

&lt;div id="d1" style="visibility: visible"&gt;
...
&lt;/div&gt;
...
&lt;input ... onclick="blinkIt()"&gt;
&lt;input ... onclick="clearTimeout(_blink)"&gt;
Copy linkTweet thisAlerts:
@ai3rulesauthorApr 26.2004 — Thank You very much.
×

Success!

Help @ai3rules spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...