How to display alert only once in a for loop?

Community Beginner ,
Jun 09, 2020

Copy link to clipboard

Copied

Is it possible  to display an alert only once in the for loop?

 

for (var i = 0; i < oMarkers.length; i++)
            {	
             if (oMarkers[i].obj.MarkerTypeId.Name == "Index")
		{
		alert ("text already present");	
				bMarkerExists = true;
				break;
			 }
            }   

 

 

Views

256

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Mentor ,
Jun 09, 2020

Copy link to clipboard

Copied

Hi, I would just use a simple boolean flag, for example:

 

var alertEnabled = true;
for (var i = 0; i < oMarkers.length; i++)
{	
  if (oMarkers[i].obj.MarkerTypeId.Name == "Index")
  {
    if(alertEnabled) alert ("text already present");	
    alertEnabled = false;
    bMarkerExists = true;
    break;
  }
}

 

I'm not sure about the whole thing, though, because I think with the 'break' statement, you will exit the loop and the alert will only appear one time anyway.

 

Hope this helps.

Russ

 

 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Beginner ,
Jun 09, 2020

Copy link to clipboard

Copied

Thank you Russ_Ward.

 

This is still showing me multiple alerts. Following is the complete code.

 

while (pgf.ObjectValid ())   {

        bMarkerExists = false;
		var marker = doc.FirstMarkerInDoc;        
        var oMarkers = pgf.GetText(Constants.FTI_MarkerAnchor);
		var alertEnabled = true;
         for (var i = 0; i < oMarkers.length; i++)
            {
             if (oMarkers[i].obj.MarkerTypeId.Name == "Index")
			 {
if(alertEnabled) alert ("Display Alert");
            alertEnabled = false;					
	    bMarkerExists = true;
	    break;
	 }
	}   

if (!bMarkerExists)
   {
		if (pgf.Name == target1.Name)   {


		}

		else if (pgf.Name == target2.Name)  {

			createMarker (doc, pgf, 0, "Index", "marker" + getText(pgf));

		}
}
pgf = pgf.NextPgfInFlow;
}
}

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jun 10, 2020

Copy link to clipboard

Copied

Your code is hard to follow because you are trying to do too much at once. As I mentioned in one of your other posts, you need to break your script down into separate tasks. I find it useful to first define what I want the entire script to do. Then break the script down into individual tasks and develop a separate function for each one.

 

Start with giving us a detailed spec on what you are trying to do with the whole script. Then we may be able to help you. Thanks.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Beginner ,
Jun 10, 2020

Copy link to clipboard

Copied

Thank you frameexpert. 

 

But I just wanted the alert to come once in the for loop. Is there a way to do this?

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Mentor ,
Jun 11, 2020

Copy link to clipboard

Copied

I agree with Rick... I am really not able to follow the logic of your code. With that break statement, your loop should terminate after the first message anyway. I'm not clear on what you are trying to do.

Likes

Translate

Translate

Report

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