Timeout Issues

4097
16
01-05-2016 11:43 AM
TomLeMahieu
New Contributor II

What I'm trying to do is zoom to the selected feature and then send the map to the printer.  Then zoom to the buffer and again print the resulting map to the printer.  Each of the following steps works by themselves.  However, I'm having a problem figuring out how to sequence the statements and use timeouts to ensure that each step is completed before the next process starts?  Here's my code:

//Zoom to Parcel                                 

setTimeout(lang.hitch(this, function () {

    this.zoomselected();

}), 3000);

setTimeout(lang.hitch(this, function () {

    html.setStyle(this.PrintMessage, 'display', 'block');

}), 5000);

            

//Zoom to Buffer

setTimeout(lang.hitch(this, function () {

    this.zoombuffer();

}), 3000);

setTimeout(lang.hitch(this, function () {

    html.setStyle(this.PrintMessage, 'display', 'block');

    this.sendToPrinter_SoilErosion(sTitle,sLayout,sFormat,sMessage);

}), 5000);

0 Kudos
16 Replies
GirishYadav
Occasional Contributor

Tom,

You need to remove the parentheses from the function call inside lang.hitch.

Wrong notation:

.then(lang.hitch(this, this.sendToPrinter_Test()));

Correct notation:

.then(lang.hitch(this, this.sendToPrinter_Test));

putting in parentheses is calling print function immediately. before the map extent gets change.

-Girish

0 Kudos
TomLeMahieu
New Contributor II

Girish,

That was the problem.  I can't believe I missed that!!

Thank you so much for all your help! I couldn't have figured it out without you.

Tom

0 Kudos
TomLeMahieu
New Contributor II

Girish,

The code you gave me works great for chaining functions together.  However, in the code below, I'm trying to step through each of the selected features and perform a set of functions.  If there is only one selected feature, it works fine.  If there are two selected features, then it only performs the zoom/print functions for the second selected feature.  That makes sense since the first chain doesn't complete it's execution before the loop steps through to the second feature.  Is there an easy way of chaining these statements inside a loop?

for (var i = 0; i < features.length; i++) {

     

      gFeature = features;

                 

      //Zoom to selected and print

      this.zoomselected()

            .then(lang.hitch(this, this.sendToPrinter_SoilErosion))

            .then(lang.hitch(this, this.zoombuffer))

            .then(lang.hitch(this, this.sendToPrinter_SoilErosion));

}

Thanks.

Tom  

0 Kudos
GirishYadav
Occasional Contributor

Tom,

I guess you can use dojo/promise/all in this scenario.

zoomToFeature(feature){
     return this.map.setExtent(feature.geometry.getExtent()) //assuming geometry is polyline or polygon
};
var promises = [];
features.forEach(function(gFeature){
     promises.push(this.zoomToFeature(gFeature)
                 .then(lang.hitch(this, this.sendToPrinter_SoilErosion))
                 .then(lang.hitch(this, this.zoombuffer))
                 .then(lang.hitch(this, this.sendToPrinter_SoilErosion)));
});

all(promises).then(function(results){
     console.log("all features printed");
});

Hopefully this will work not sure though. Just give it a try.

-Girish

0 Kudos
TomLeMahieu
New Contributor II

Girish,

Hmmm...I select 2 parcels and push the statements to the promises array.

The alert on the promises length is 2. When the "all(promises).then" it just executes the statements (zoom to parcel, print, zoom to buffer, print) for the second selected parcel. It's doing everything correctly except chaining the two promises together.  Any ideas? Am I missing something?

//Push the statements to promises

var promises = [];

var features = this.selValueShapeFeatures;

for (var i = 0; i < numSelected; i++) {

      this.gFeature = features;

      alert(features.attributes["PIN"]);

      promises.push(this.zoomToFeature(this.gFeature)

            .then(lang.hitch(this, this.sendToPrinter_SoilErosion))

            .then(lang.hitch(this, this.zoombuffer))

            .then(lang.hitch(this, this.sendToPrinter_SoilErosion))

      );

}

               

//Number of records in promises

alert(promises.length);

               

//Execute promises              

all(promises).then(function(results){

      console.log("all features
printed"
);

});

Thanks.

Tom

0 Kudos
ChrisSmith7
Frequent Contributor

I wish I would have employed this a couple of years back - I ended-up creating a bunch of kludgy, asynch watches on variables and stuff for flow control. It works well, but sure is a strange way of doing things... Now that I'm a bit wiser, I think a refactoring is in order...

GirishYadav
Occasional Contributor

Yeah, promises are a really cool stuff and much cleaner way to write asyc code. Now its gonna be in limelight in ArcGIS JS API 4.0

0 Kudos