Skip to main content
Participant
June 9, 2020
Question

How to display alert only once in a for loop?

  • June 9, 2020
  • 1 reply
  • 2618 views

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

 

 

    This topic has been closed for replies.

    1 reply

    Legend
    June 9, 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

     

     

    Participant
    June 10, 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;
    }
    }
    frameexpert
    Community Expert
    Community Expert
    June 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.

    www.frameexpert.com