Turning off Math.random() after one use?

New Here ,
May 13, 2021 May 13, 2021

Copy link to clipboard

Copied

Hi, I currently have a 5 product banner with a randomizer and next/previous buttons. When it launches, it loads one of those frames using the Math.random() command. However, it does it again when either of the next & previous buttons are clicked. Is there any way for me to turn that off after it's been executed once at launch?

 

For example:

  • HTML randomly loads a frame from 1 to 5 (E.g. Frame 3).
    I'd like the next button to continue in sequential order from there: 3, 4, 5, 1, 2, 3

 

  • HTML randomly loads Frame 4.
    The next button continues sequetionally when clicked: 4,5,1,2,3,4

 

Here's what I have in Frame 0:

 

var nextFrame = 1;
var totalFrames = 5;
var randomFrame = Math.random()*5;
var lastPicked = randomFrame - 1;

this.next.addEventListener("click", fl_ClickToGoToAndStopAtFrame.bind(this));

function fl_ClickToGoToAndStopAtFrame()
{
	nextFrame++;
	if (nextFrame > totalFrames) {
	nextFrame = 1;
}
	this.gotoAndStop(nextFrame);
}

this.previous.addEventListener("click", fl_ClickToGoToAndStopAtFrame.bind(this));

function fl_ClickToGoToAndStopAtFrame()
{
	nextFrame--;
	
	if(nextFrame < 1) {
	nextFrame = totalFrames;
}
	this.gotoAndStop(nextFrame);
}

this.gotoAndPlay(randomFrame);
{
	if (randomFrame == currentFrame){
		randomFrame = lastPicked;
	}
}

 

And on Frame 1 (the first product), I have this:

this.stop();

/* Product 1 Link */

this.product1_cta.addEventListener("click", fl_ClickToGoToWebPage);

function fl_ClickToGoToWebPage() {
	window.open("https://www.google.com", "_blank");
	
	if(!this.started) {
		this.product1_cta.addEventListener("click", fl_ClickToGoToWebPage);
		this.started = true;
	}
}

 

I'm still learning, so sorry if this isn't right. I'm using the code snippet and action wizard to adjust as I need to.

 

Thank you!

Views

90

Likes

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
Adobe Community Professional ,
May 13, 2021 May 13, 2021

Copy link to clipboard

Copied

LATEST

Hi.

 

I have a suggestion. Please see if this works for you:

var root = this;

root.main = function()
{
	root.stop();
	root.gotoAndStop(Math.floor(Math.random() * root.totalFrames));
	root.previous.on("click", root.navigate, root, false, { offset: -1 });
	root.next.on("click", root.navigate, root, false, { offset: 1 });
};

root.navigate = function(e, data)
{
	var destination = root.currentFrame + data.offset;
	root.gotoAndStop(destination > -1 ? destination : root.totalFrames -1);
};

if (!root.started)
{
	root.main();
	root.started = true;
}

 

Regards,

JC

Likes

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