Reputation: 6646
Is it possible Collecting data from another function while function running ?
// common function
function collectingData(){
var name = 'tom';
return name
}
// my target is here
$('.myDiv').click(function(){
// here I want data from another function
// collectingData()
if( name == 'tom' ){
//here I have to finish based of data
}
})
Upvotes: 0
Views: 77
Reputation: 101
Call like this:-
function collectingData(){
name = 'tom';
return name
}
$('.myDiv').click(function(){
// here I want data from another function
//
var name ='tom'
if( name == collectingData() ){
//here I have to finish based of data
}
})
Upvotes: 1
Reputation: 1125
Why dont you make
namea global variable by removing the preceding
var` :
function collectingData(){
name = 'tom';
// return name
}
name automatically appears in the whole script
$('.myDiv').click(function(){
// here I want data from another function
// collectingData()
if( name == 'tom' ){
//here I have to finish based of data
}
})
Upvotes: 1
Reputation: 2201
You already return name from collectingData(); so, define a variable, do what ever stuff you want to do,
$('.myDiv').click(function(){
// here I want data from another function
// collectingData()
var name = collectingData();
if( name == 'tom' ){
//here I have to finish based of data
}
})
Upvotes: 1
Reputation: 10179
You can! As long as the function which you collect data is not an asynchronous function.
In case it is an asynchronous function, then depending on the return data of that function, we will use an appropriate approach to deal with that return data.
There are two common patterns to deal with asynchronous function: callback-based and promise-based.
Upvotes: 1
Reputation: 858
I'm new so I can't post this as a comment, but is there a reason you can't run collectingData()
within the click handler, as let name = collectingData()
? That would be the easiest way to pass the data between the two.
Upvotes: 1
Reputation: 169
Try this
$('.myDiv').click(function(){
// here I want data from another function
// collectingData()
let name = collectingData();
if( name == 'tom' ){
//here I have to finish based of data
}
})
Upvotes: 1