如果有条件只执行一次如何执行

时间:2016-04-24 23:19:21

标签: javascript

我想知道我是怎么做到的,因为变量一直都会返回默认值。这是一个Ibeacon应用程序,我不知道是否需要显示有关我的应用程序的更多详细信息。我只想把这个功能叫做一次,有人能帮助我吗?



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 to 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);
		});
	},
};
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

使用localStoragehttps://developer.mozilla.org/en-US/docs/AJAX/Getting_Started

localStorage(和sessionStorage)可让您保留持久值

function something() {
    localStorage.setItem("somethingCalled", "yes");
    if(quit) {
       window.open("info.html");
    } 
    quit = true;
}

然后你打电话给something()

if (localStorage.getItem("somethingCalled")!="yes") { 
    something()
}

您可能想要使用sessionStorage代替localStoragehttps://developer.mozilla.org/en/docs/Web/API/Window/localStorage