0

I have two different functions which gets parameters:

push.on('registration', function(data) {
        var id = data.registrationId;
        });


push.on('notification', function (data) {
        var count = data.count;
        });

Now I want to use the variables id and count in another new function:

function three(id, count){

var _id = id;
var _count = count;

}

How is that possible?

2
  • Now I want to use the variables id and count in another new function: in one of these events or will there be a third event? Commented Feb 25, 2016 at 11:47
  • Read this: stackoverflow.com/a/24544538 Commented Feb 25, 2016 at 11:50

4 Answers 4

3
var id, count;

push.on('registration', function(data) {
    id = data.registrationId;
});


push.on('notification', function (data) {
    count = data.count;
});

Now you can invoke three(id, count). The issue is that you'll have to wait until both values are present before calling three. Probably you're looking for something along these lines:

var id, count;

push.on('registration', function(data) {
    id = data.registrationId;
    callThree();
});


push.on('notification', function (data) {
    count = data.count;
    callThree()
});

function callThree() {
    if (id && count) {
        three(id, count);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I think your answer is perfect, but if I try to output the variable within function three the alert is "undefinied" :-( I don't understand why... I will investigate and let you know.
0
push.on('registration', function(data) {
    var id = data.registrationId;
    push.on('notification', function (data) {
        var count = data.count;
        three(id,count)
    });

 });

UPDATE BASED ON COMMENT : Assuming the order of event,

Comments

0

If you aren't sure what order the events will occur, you may need to get id and count outside their function scopes and then, when one push function fires, check to see if the other has, and if so, call your three(id, count). One way, is to just declare the variables outside their functions, and then update their value accordingly.

var id = null;
var count = null;

push.on('registration', function(data) {
        id = data.registrationId;
        count !== null ? three(id, count) : null
        });


push.on('notification', function (data) {
        count = data.count;
        id !== null ? three(id, count) : null
        });

Comments

0

The reason why you can't use 'id' and 'count' variables in function three() is because they are declared within the scope of the individual functions. To use them in function three() simply move it to a higher scope.

Here's how I would approach your problem:

var id, count;

function one() {
    push.on('registration', function(data) {
         id = data.registrationId;
    });
}

function two() {
    push.on('notification', function (data) {
        count = data.count;
    });
}

function three(){
    var _id = id;
    var _count = count;
}

Comments

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.