Time delay within a loop?

Hello,
I’m trying to delay showing text value at 0.1 minute intervals. See code below. This only reports the end loop value of 10.
Q1. what should the code be to show each 0.1 min increment?
Q2. how do I setup min+0.1 rather than min++ ? Tried but doesn’t work.

I’m a complete novice - so all help gratefully received. Thanks !

let min = $w ( ‘#myText’ ). text
setTimeout ( function () {
$w ( ‘#myText’ ). text = “” + min
for ( let min = 0 ; min < 10.01 ; min ++){
$w ( ‘#myText’ ). text = “” + min
console . log ( min )
}
}, 6000 );

You might want to use setInterval() instead.

Thanks for your response. I have tried setInterval(), but got the same result.
I’m guessing that my formatting is incorrect? I’m still stuck in BBC basic if/then/next/goto !

let min = $w ( ‘#myText’ ). text
//setInterval(() => {
$w ( ‘#myText’ ). text = “” + min
for ( let min = 0 ; min < 10.01 ; min ++){
setInterval (() => {
$w ( ‘#myText’ ). text = “” + min
console . log ( min )
}
, 6000 )

typo error in last post - trying various options.
Code is as below & still only gives the final result of 10

let min = $w ( ‘#myText’ ). text
setInterval (() => {
for ( let min = 0 ; min < 10.01 ; min ++){
$w ( ‘#myText’ ). text = “” + min
console . log ( min )
}
}
, 6000 )

I’m really not sure what it is you’re trying to do. Are you perhaps trying to create a Typewriter effect?

I’m trying to make a simple minute timer, that changes every 0.1 minute. I’ve seen examples of month/week/day/hr/min/sec countdown code but I only need a minute timer.

Aha! Gotcha. You want something like this:

    let current = 0.0;
    let count = setInterval(() => {
        current += 0.1;
        if (current.toFixed(1) === '1.0') {
            clearInterval(count);
        }
        console.log(current);
        $w('#text1').text = "" + current.toFixed(1);
    }, 6000)

I hope this helps. For something more robust, you can see the Count down/up example.

Thanks Yisrael. I think you’ve guessed my next question…how do I stop at a given time and reset to 0.0 ?
For a complete newbie, what website javascript / wix vel learning resource would you recommend?

In my snippet I show how to stop (clearInterval) after arriving at a minute. Just call the same code to start again at 0.0. I would suggest looking at the Count down/up example to see how to set a time (and date) as a target for counting up or down.

To learn about programming with Velo, read the following articles that will help you start working with Velo:

And here are a couple of good sites to help you increase your knowledge of Javascript:

Have fun learning, and know that the Velo community is always here for you when you have questions.

Thanks. It seems that one problem solved = new problem ! I’m obviously not in a ‘coder’ mindset yet !

I can stop at a given time point, but I’m unable to reset the text to 0.0 at the end. Am I using wrong code or ( or { in the wrong place ?

$w ( ‘#myText’ ). text = “0.0”
let current = 0.0 ;
let end = 0.2
let minReset = 0.0
let myTimer ;
myTimer = setInterval (() => {
current += 0.1 ;
$w ( ‘#myText’ ). text = “” + current . toFixed ( 1 );
current == end && clearInterval ( myTimer );
console . log ( current );
}, 6000 );

$w ( '#myText' ). text = "0.0" ;

Sorted - got it !
I’m on a steep (frustrating at times) learning curve - but enjoying it.
Forum help & examples are extremely useful - thank you.

You need to put the code in a function, and then call it whenever you want to start the count again. However, you should keep in mind that the setInterval() function runs in the background until it finishes its count. Once it’s finished, you can call it again.