0

This may seem simple to some but I am less experienced with JavaScript. I have two functions. One is called when my upload begins. The next is called when my upload ends.

In the first function, a variable is created, a unique id used for the upload. What is the best way to go about reusing it in my second function since it is not global? The reason I defined it within my function is because every time a user clicks the upload button, the function is called and a NEW id is created for that upload, that is why I do not want to define it outside because then the same id would be served for a second upload unless the page is refreshed.

Anyone got any suggestions?

function uploadstart() {

    function makeid() {
        var text = "";
        var possible = "abcdefghijklmnopqrstuvwxyz0123456789";

        for( var i=0; i < 32; i++ )
            text += possible.charAt(Math.floor(Math.random() * possible.length));

        return text;
    }

    var rand_id = makeid();

}

uploadfinish(){
    //rand_id will be undefined
}
Patrick McElhaney
  • 57,901
  • 40
  • 134
  • 167
Scarface
  • 3,845
  • 7
  • 41
  • 71

4 Answers4

3

Pass in that var as a parameter

uploadstart(){
function makeid()
{
    var text = "";
    var possible = "abcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 32; i++ )
    text += possible.charAt(Math.floor(Math.random() * possible.length));

return text;
}
var rand_id=makeid();

//Pass in rad_id
uploadfinish(rand_id);
}

uploadfinish(radomID){
//rand_id will be undefined
}
Soatl
  • 10,224
  • 28
  • 95
  • 153
  • +1 for answer, but I am actually calling the uploadfinish function externally (because there are other functions and order I am maintaining) so this would not really be feasible. – Scarface Jul 21 '11 at 17:35
  • use this logic anyway, i mean you can chain-pass the value from a functon to the next one, until the last one will pass the value to uploadfinish()... – Matteo Bononi 'peorthyr' Nov 06 '13 at 14:54
2

Try declaring rand_id in global scope (before everything)

var rand_id;
function bla....
ChristopheCVB
  • 7,269
  • 1
  • 29
  • 54
  • This will work as long as you are not uploading more than one item at a time, and you make sure the invocation of the `uploadfinish()` is sequentially after the call to `makeid()` (ie. somewhere outside the JS or in a deferred callback) because I don't think Javascript necessarily executes sequentially. – Evan Layman Jul 21 '11 at 17:32
  • Yep, you're right, but we don't know the real context of his script... It can be enough in this case... – ChristopheCVB Jul 21 '11 at 17:34
  • Are you uploading multiple files at same time ? – ChristopheCVB Jul 21 '11 at 17:36
  • no I am not, the script will only be so multiple users can upload one file at a time. – Scarface Jul 21 '11 at 18:16
1

The solution to this problem depends on how and where you use those two functions. If you pass them as callbacks to another function from some ajax library, or something like that, and if you control that library call, you could use a closure.

So, if for example you do something like this when an upload is begun:

Library.foo(..., uploadstart, uploadfinish);

You could define makeID as a global function, and then bind the generated id to your callbacks using a function like this:

function bind_id(rand_id, my_function) {      
  return function() { // return a closure
    return my_function(); // my_function is executed in a context where rand_id is defined
  }
}

Then you define your callbacks using rand_id as if it were global (actually, it will defined in the closure):

function uploadstart() {
  // use rand_id as you wish
}

function uploadend() {
  // use rand_id as you wish
}

When you need to call your Library.foo function, first generate the rand_id, then bind it to the start and end callbacks:

var new_rand_id = randID();

Library.foo(..., bind_id(new_rand_id,uploadstart), bind_id(new_rand_id,uploadend));

This way you'll pass to foo not the original uploadstart and uploadend, but two closures where rand_id is defined and i the same for both, so that callback code can use that variable.

PS: closures are one of the most powerful and trickiest features of javascript. If you're serious about the language, take your time to study them well.

Dan Mazzini
  • 1,005
  • 10
  • 19
0

What do you do with the rand_id once you've created it? Could you not just call uploadfinish with the rand_id as a parameter?

function makeid()
{
   ...
}
var rand_id=makeid();
uploadfinish(rand_id);
}

uploadfinish(id){
//rand_id will be 'id'
}

[EDIT] Since you said you need to call the function externally, check out this page for details about callbacks:Create custom callbacks

function doSomething(callback) {
    // ...

    // Call the callback
    callback('stuff', 'goes', 'here');
}

function foo(a, b, c) {
    // I'm the callback
    alert(a + " " + b + " " + c);
}

doSomething(foo);

That will call doSomething, which will call foo, which will alert "stuff goes here".

Note that it's very important to pass the function reference doSomething(foo), rather than calling the function and passing its result like this: doSomething(foo()).

Community
  • 1
  • 1
Evan Layman
  • 3,691
  • 9
  • 31
  • 48
  • Not sure why I get a -1 while Peppered Lemons had the same answer.. Oh well. – Evan Layman Jul 21 '11 at 17:38
  • 1
    +1 for effort. Christophers answer is enough but you do not deserve a -1 lol. Appreciate the contribution. – Scarface Jul 21 '11 at 18:17
  • Thanks. Good luck! I'm a novice javascripter so I hope it all works for you. I just remember having many functions not behaving as I'd expect because of out-of-order execution. – Evan Layman Jul 21 '11 at 18:19