1

I have a src js file where I have this code. I need to manage the est_bandwidth array in the script but even if I'm sure of the correct creation I can't see globally. Why? How can I see the array also outside of the function?

var maxBandwidth = 8 * 1024 * 1024;        // 8 Mbps
var est_bandwidth = new Array();
function bandwidth(initial_bps, weight_f, weight_s){

this.bps = initial_bps;
this.weight_f = weight_f;
this.weight_s = weight_s;

}

bandwidth.prototype.calcWeightedBandwidth = function(_bps) {

this.bps = parseInt(((this.weight_f*this.bps) + (this.weight_s * _bps))/2)*0.9;  
    if( this.bps > maxBandwidth && maxBandwidth > 0) this.bps = maxBandwidth;
    est_bandwidth.push(this.bps/1024);
    return this.bps;
}
15
  • Where is est_bandwidth declared? I can't see an initialization - I'd expect this to throw an error. Commented Dec 14, 2012 at 12:22
  • I just forgot the initialization in the example, now I've edited the code here but same problem: I see an empty array even if it's not empty. Commented Dec 14, 2012 at 12:28
  • Ah, Ok. And this is the whole js file? How do you access it from outside? Commented Dec 14, 2012 at 12:28
  • I have an html page where I have <script src="bandwidth.js"></script> Commented Dec 14, 2012 at 12:34
  • this bandwidth.js interact with other js and I want to be able to do something like this <script> document.getElementById("elem").innerHTML=est_bandwidth </script> Commented Dec 14, 2012 at 12:36

1 Answer 1

1

Declare est_bandwidth variable outside of the function to make it available globally. Use var est_bandwidth;

Sign up to request clarification or add additional context in comments.

2 Comments

I've declared the variable but nothing changed, I see an empty array
Are you certain that code that is suppose to fill the array with data does actually run? Put an alert just before est_bandwidth.push to confirm that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.