javascript - How to execute if conditional just once -
i wondering me how that, because time variable returns default value. it's ibeacon application, don't know if need show more details app. want call function once, can me?
function uint8arrtohexstringnospace(arr) { return array.prototype.map.call(arr, function(n) { var s = n.tostring(16); if(s.length == 1) { s = '0'+s; } return s; }).join(''); } var quit; function something() { if(quit) { window.open("info.html"); } quit = true; } function appendtd(root, value, id) { var text = document.createtextnode(value); var td = document.createelement("p"); if(id) { td.id = id; } td.appendchild(text); root.appendchild(td); } function hex16(i) { var s = i.tostring(16); while(s.length < 4) { s = '0'+s; } return s; } var beacons = {}; var app = { initialize: function() { // important stop scanning when page reloads/closes! window.addeventlistener('beforeunload', function(e) { ibeacon.stopscan(); }); this.bindevents(); }, bindevents: function() { document.addeventlistener('deviceready', this.ondeviceready, false); }, ondeviceready: function() { //app.receivedevent('deviceready'); app.startscan(); }, receivedevent: function(id) { var parentelement = document.getelementbyid(id); var listeningelement = parentelement.queryselector('.listening'); var receivedelement = parentelement.queryselector('.received'); listeningelement.setattribute('style', 'display:none;'); receivedelement.setattribute('style', 'display:block;'); console.log('received event: ' + id); }, startscan: function() { ibeacon.startscan({}, function(beacon) { //console.log("beacon found: "+beacon.address+" "+beacon.name+" "+beacon.rssi+"/"+beacon.txpower); var r = beacon.region; //console.log("m"+r.major.tostring(16)+" m"+r.minor.tostring(16)+" uuid "+uint8arrtohexstringnospace(r.uuid)); var key = 'tx'+beacon.address.replace(/:/g,'_'); //console.log('key: '+key); if(beacons[key] == null) { beacons[key] = beacon; var root = document.getelementbyid("beaconlistroot"); var tr = document.createelement("tr"); var br = document.createelement("br"); // <tr><td>address</td><td>name</td><td>rssi</td><td>id</td><td>uuid</td></tr> var adress = ' adress: '; var name = ' name: '; var distance = ' distance: '; var major = ' major: '; var minor = 'minor: '; var uuid = ' uuid: '; appendtd(tr, adress + beacon.address + name + beacon.name); appendtd(tr, distance + beacon.rssi+" /"+beacon.txpower+"\u00a0"+beacon.estimateddistance.tofixed(2)+'m', key); appendtd(tr, major + hex16(r.major)+"\u00a0"+ minor +hex16(r.minor)); appendtd(tr, uuid + uint8arrtohexstringnospace(r.uuid)); root.appendchild(tr); } else { var td = document.getelementbyid(key); td.firstchild.nodevalue = 'power: ' + beacon.rssi+"/"+beacon.txpower+ ' distance: ' + "\u00a0"+beacon.estimateddistance.tofixed(2)+'m'; } if(beacon.address == '78:a5:04:13:3b:17' && beacon.estimateddistance.tofixed(2) <= 10 ){ something(); } }, function(error) { console.log("startscan error: " + error); }); }, };
use localstorage
: https://developer.mozilla.org/en/docs/web/api/window/localstorage
localstorage
(and sessionstorage
) let keep persistent values
function something() { localstorage.setitem("somethingcalled", "yes"); if(quit) { window.open("info.html"); } quit = true; }
then call something()
:
if (localstorage.getitem("somethingcalled")!="yes") { something() }
you may whant use sessionstorage
instead of localstorage
(https://developer.mozilla.org/en-us/docs/web/api/window/sessionstorage)
Comments
Post a Comment