Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store fingerprint2 result in a var

Tags:

javascript

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
});
like image 776
محمد علی پور فتحکوهی Avatar asked Sep 29 '16 11:09

محمد علی پور فتحکوهی


1 Answers

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);
}
like image 74
iblamefish Avatar answered Nov 12 '22 07:11

iblamefish