rpgs_player
rpgs_player

Reputation: 5449

Trouble using persist.progressListener

What I'm trying to do is to modify HTML file downloaded from persist.saveDocument(). It means, I have to know when the file has been downloaded successfully. So I need to implement a nsIWebProgressListener in persist.progressListener.

Here's my code:

persist.progressListener = {
  onProgressChange: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress) {
  },
  onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus) {
    if(aStateFlags & stop) {
      alert("file has been downloaded");
    }
  }
}

The problem is, alert function is executed countless times (even sometimes I have to force close the browser) instead of just once. Does anyone know how to execute alert only once?

Upvotes: 0

Views: 46

Answers (1)

rpgs_player
rpgs_player

Reputation: 5449

Solved it. I added boolean variable to the if statement, so the code will look like this:

persist.progressListener = {
  abc : true,
  onProgressChange: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress) {
  },
  onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus) {
    if((aStateFlags & stop) && abc == true) {
      this.abc = false;
      alert("file has been downloaded");
    }
  }
}

The reason why the alert function is executed repeatedly is because (aStateFlags & stop) almost always returns 'truthy' value (value other than 0), so I added a boolean variable to force the condition to result in false (truthy and false will result in false).

Upvotes: 1

Related Questions