0 votes
by (190 points)

I want to display some text under if conditions are satisfied:

<<set $value to 0>>
<<set $value_completed to false>>

<p id="value">$value OK</p>
<<repeat 0.125s>>
    <<set $value to $value + 32>>
    <<if $value <= 640>>
        <<replace "#value">>$value OK<</replace>>
        <<else>>
        <<set $value_completed to true>>
        <<stop>>    
    <</if>>
<</repeat>>

<<if $value_completed == true>>
<p>It works!</p>
<</if>>

But it doesn't work. Any help?

Thanks in advance!

2 Answers

+1 vote
by (44.7k points)
selected by
 
Best answer

The <<repeat>> macro works asynchronously, so things after it will be displayed immediately.

You can fix your code like this:

<<set $value to 0>><span id="value">$value OK</span>
<<silently>><<repeat 0.125s>>
	<<set $value to $value + 32>>
	<<if $value <= 640>>
		<<replace "#value">>$value OK<</replace>>
	<<else>>
		<<replace "#done">>It works!<</replace>>
		<<stop>>    
	<</if>>
<</repeat>><</silently>>
<span id="done"></span>

Also, using <span> or <div> instead of <p> should keep it from ending up with a ton of blank space between elements.

Hope that helps!  :-)

0 votes
by (2.4k points)

I think the problem is that Sugarcube only check the <<if>> conditions once when the passage is loaded, so the $value_completed == true is only checked once when it still is false. Forcing it to be checked again with another <<repeat>> does the trick, but I think it could cause problem with bigger conditions or something like that

<<repeat 0.125s>>
	<<if $value_completed == true>>
	<p>It works!</p>
	<<stop>>
	<</if>>
<</repeat>>

 

...