javascript - How to store fingerprint2 result in a var - Stack Overflow

I'm tying to store fingerprint2 result in a var.var info = {};new Fingerprint2().get(function(res

I'm tying to store fingerprint2 result in a var.

var info = {};
new Fingerprint2().get(function(result, ponents){
  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 /

Usage:

new Fingerprint2().get(function(result, ponents){
  console.log(result); //a hash, representing your device fingerprint
  console.log(ponents); // an array of FP ponents
});

I'm tying to store fingerprint2 result in a var.

var info = {};
new Fingerprint2().get(function(result, ponents){
  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, ponents){
  console.log(result); //a hash, representing your device fingerprint
  console.log(ponents); // an array of FP ponents
});
Share Improve this question asked Sep 29, 2016 at 11:21 محمد علی پور فتحکوهیمحمد علی پور فتحکوهی 4135 silver badges20 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

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, ponents) {
    info.fingerprint = result;

    afterFingerprintIsCalculated();
});

function afterFingerprintIsCalculated() {
    alert(info.fingerprint);
}



// BETTER (no global state)
new Fingerprint2().get(function(result, ponents) {
    var info = {
        fingerprint: result
    };

    processFingerprint(info);
});

function processFingerprint(data) {
    alert(data.fingerprint);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743748557a4500469.html

相关推荐

  • javascript - How to store fingerprint2 result in a var - Stack Overflow

    I'm tying to store fingerprint2 result in a var.var info = {};new Fingerprint2().get(function(res

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信