We are having difficulty assigning a forge.prefs value to a variable, so that we can pass it on as a parameter.

This is a sample call:

forge.prefs.get('offset_val' function(offset1){
     offset = offset1; 
})
forge.prefs.get('id', function(val){
     uid = val;
})

load_my_car(uid,offset);

It is returning undefined and it is very inconvenient to call it in a nested prefs command.

Can someone help us regarding this problem?

有帮助吗?

解决方案

forge.prefs.get() is probably an asynchronous function call, which means that its callback is executed somewhat delayed. In your example load_my_car() is executed before the two callbacks are fired, so the variables are still undefined.

You have to make sure that the callbacks are fired before calling load_my_car(), try this:

forge.prefs.get('offset_val' function(offset1){
  forge.prefs.get('id', function(val){
    load_my_car(val,offset1);
  }); 
});

If you really don't want to have two nested forge.prefs.get() you'd need to check which callback finishes first and then only call load_my_car() after the second finished.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top