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

How to display alert only once in a for loop?

Community Beginner ,
Jun 09, 2020 Jun 09, 2020

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;
			 }
            }   

 

 

2.4K
Translate
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
Mentor ,
Jun 09, 2020 Jun 09, 2020

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

 

 

Translate
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 ,
Jun 09, 2020 Jun 09, 2020

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;
}
}
Translate
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 ,
Jun 10, 2020 Jun 10, 2020

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.

Translate
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 ,
Jun 10, 2020 Jun 10, 2020

Thank you frameexpert. 

 

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

Translate
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
Mentor ,
Jun 11, 2020 Jun 11, 2020
LATEST

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.

Translate
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