• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Delaying the running of a FOR loop using JavaScript in canvas - simulating a roll of a dice.

Community Beginner ,
Apr 11, 2020 Apr 11, 2020

Copy link to clipboard

Copied

I am trying to simulate a dice roll in an activity for my class. I can generate a random number between 1 and 6 and place it in a textbox on the canvas.

What I need help with is how to display a series of numbers in the text box before it stops at the final numnber (sort of like a dice roll)

 

Here is where I have got to:

 

function rolldice(){

     for (i=0; i<10; i++){.    // the idea here would be to display a series of 10 random numbers between 1 and 6 with a short pause in between.

          this.num1.text=randomIntFromInterval(1,6).      //this is a function I use and it generates the random number. num1 is the name of the text field.

          setTimeout ....... //this is where I have trouble ... I don't know if I am using the correct idea.

     }

}

 

I wonder if someone can give me a hand with this.

 

Thank you

 

TOPICS
Code , How to

Views

636

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 11, 2020 Apr 11, 2020

Hi.

 

You can use an interval and a variable to keep track of the number of turns. Like this:

 

var root = this;
var txt = root.num1;
var delay = 1000;
var maxIntervals = 10;
var count = 0;

root.randomIntFromInterval = function()
{
	return Math.ceil(Math.random() * 6);
};

root.interval = setInterval(function()
{
	txt.text = String(root.randomIntFromInterval());
	count++;
	
	if (count === maxIntervals)
		clearInterval(root.interval);
}, delay);

 

 

Please let us know if you need further assista

...

Votes

Translate

Translate
Community Expert ,
Apr 11, 2020 Apr 11, 2020

Copy link to clipboard

Copied

JoãoCésar is really good with issues like this he may be able to help.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 11, 2020 Apr 11, 2020

Copy link to clipboard

Copied

Hi.

 

You can use an interval and a variable to keep track of the number of turns. Like this:

 

var root = this;
var txt = root.num1;
var delay = 1000;
var maxIntervals = 10;
var count = 0;

root.randomIntFromInterval = function()
{
	return Math.ceil(Math.random() * 6);
};

root.interval = setInterval(function()
{
	txt.text = String(root.randomIntFromInterval());
	count++;
	
	if (count === maxIntervals)
		clearInterval(root.interval);
}, delay);

 

 

Please let us know if you need further assistance.

 

 

Regards,

JC

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 11, 2020 Apr 11, 2020

Copy link to clipboard

Copied

Well that works really well. There is no way I would have discovered how to do that without your help.

Thank you very very much JC.

 

I am now going to work at setting this up so that it runs with a button click .... my next challenge!!!!!

 

Kind regards.....

Rod

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 13, 2020 Apr 13, 2020

Copy link to clipboard

Copied

LATEST

You're welcome, Rod!

 

I wish success in your next challenge!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines