I'm tying to store fingerprint2 result in a var.
var info = {};
new Fingerprint2().get(function(result, components){
info.fingerprint = result;
});
alert(info.fingerprint);
but not work is there a better way like:
var fp = new Fingerprint2().get();
or some enhanced method?
edited : Modern & flexible browser fingerprinting library, a successor to the original fingerprintjs http://valve.github.io/fingerprintjs2/
Usage:
new Fingerprint2().get(function(result, components){
console.log(result); //a hash, representing your device fingerprint
console.log(components); // an array of FP components
});
Your code is not working as expected because Fingerprint2().get(...)
is asynchronous - that means that alert(info.fingerprint)
will run before the callback function passed to get
has finished.
To understand why this is necessary if you don't understand the concept of asynchonicity, take a look at the demo page. You'll see that it says how long it takes to calculate the fingerprint. You alert
will be run between when get
is called and when the has been calculated, and therefore cannot know what the fingerprint will be.
The only time that you can guarantee that you have the fingerprint is during the callback function.
var info = {};
new Fingerprint2().get(function(result, components) {
info.fingerprint = result;
afterFingerprintIsCalculated();
});
function afterFingerprintIsCalculated() {
alert(info.fingerprint);
}
// BETTER (no global state)
new Fingerprint2().get(function(result, components) {
var info = {
fingerprint: result
};
processFingerprint(info);
});
function processFingerprint(data) {
alert(data.fingerprint);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With