[英]Javascript Switch function return undefined
I have a simple quiz app, with certain clicks you get rating.我有一个简单的测验应用程序,您可以通过某些点击获得评分。
function 1() = javascript switch statement that render rating ( low, medium, high) function 1() = javascript 渲染等级(低、中、高)的开关语句
this function calculate the rating based on click count.此 function 根据点击次数计算评分。 it works good
效果很好
then I have function color() that supposed get the rating result of function 1 and style it.然后我有 function color() 应该得到 function 1 的评级结果并对其进行样式设置。 however, function 2 always render undefined simply it is executed before function 1 is done.
然而, function 2 总是呈现未定义,它只是在 function 1 完成之前执行。
I am sure I am writing the callback function incorrect.我确定我写的回调 function 不正确。
<script type="text/javascript">
var clicks = 0;
var user = dish;
function onClick() {
clicks += 1;
document.getElementById("clicks").innerHTML = clicks;
switch (clicks) {
case 0:
case 1:
case 2:
document.getElementById("dish").innerHTML = 'LOW.';
document.getElementById("dish").style.color = '#90ee90';
// code block
break;
case 3:
case 4:
case 5:
document.getElementById("dish").innerHTML = 'MEDIUM.';
document.getElementById("dish").style.color = '#ffbf00';
// code block
break;
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
document.getElementById("dish").innerHTML = 'HIGH.';
document.getElementById("dish").style.color = '#ff5500';
// code block
break;
default:
// code block
document.getElementById("dish").innerHTML = 'HELLO.';
}
};
function onClickdouble() {
clicks += 2;
document.getElementById("clicks").innerHTML = clicks;
switch (clicks) {
case 0:
case 1:
case 2:
document.getElementById("dish").innerHTML = 'LOW.';
document.getElementById("dish").style.color = '#90ee90';
// code block
break;
case 3:
case 4:
case 5:
document.getElementById("dish").innerHTML = 'MEDIUM.';
document.getElementById("dish").style.color = '#ffbf00';
// code block
break;
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
document.getElementById("dish").innerHTML = 'HIGH.';
document.getElementById("dish").style.color = '#ff5500';
// code block
break;
default:
// code block
document.getElementById("dish").innerHTML = 'HELLO.';
}
};
function noClick() {
clicks += 0;
document.getElementById("clicks").innerHTML = clicks;
switch (clicks) {
case 0:
case 1:
case 2:
document.getElementById("dish").innerHTML = 'LOW.';
document.getElementById("dish").style.color = '#90ee90';
// code block
break;
case 3:
case 4:
case 5:
document.getElementById("dish").innerHTML = 'MEDIUM.';
document.getElementById("dishr").style.color = '#ffbf00';
// code block
break;
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
document.getElementById("dish").innerHTML = 'HIGH.';
document.getElementById("dish").style.color = '#ff5500';
// code block
break;
default:
// code block
document.getElementById("holder").innerHTML = 'HELLO.';
}
}
console.log((user).value);
</script>
<script type = "module">
/
function color() {
var user = document.getElementById("dish").innerHTML;
if (user === 'LOW.') {
return '#0DEAD0';
} else if (user === 'MEDIUM.') {
return '#FFCC00';
} else {
return '#F30B0B';
}
}
console.log(color());
var svg = '<svg height="10" width="10"><circle cx="5" cy="5" r="4" fill="' + color() + '" /></svg>';
</script>
I cannot get the innerhtml (result dish of the quiz rating) so I can style it accordingly.我无法获得 innerhtml(测验评分的结果盘),因此我可以相应地对其进行样式设置。 and show it on the map as it is executed before the switch function render a result.
并在 map 上显示它,因为它在开关 function 呈现结果之前执行。
any tip or hint will be highly appreciated.任何提示或提示将不胜感激。
//get location
var map = L.map('map').fitWorld();
const tile = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png?{foo}',
{foo: 'bar', attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'});
// Socket Io
const socket = io.connect();
// icon color
var color = function(){
var user = document.getElementById("holder").innerHTML;
if (user == 'LOW.') {
return '#0DEAD0'
}else if (user == 'MEDIUM.' ) {
return '#FFCC00'
}else {
return '#F30B0B'
}
}
function setMap(){
if(document.getElementById("holder").offsetParent != null){
console.log("working now");
document.getElementById("holder").style.color = color();
var svg = '<svg height="10" width="10"><circle cx="5" cy="5" r="4" fill="' + color() + '" /></svg>';
console.log(svg);
var icon = L.divIcon({ html: svg, iconSize: [10, 10], iconAnchor: [10, 10] });
//location
map.locate({setView: true, watch: true, enableHighAccuracy: true, maxZoom: 8});
map.on('locationfound', e => {
const coords = [e.latlng.lat, e.latlng.lng];
const newMarker = L.marker(coords, {icon:icon});
newMarker.bindPopup("Wear your Mask!");
map.addLayer(newMarker);
socket.emit('userCoordinates', e.latlng);
});
//socket new User connected
socket.on('newUserCoordinates', (coords) => {
console.log(coords);
const userIcon = L.icon({
iconUrl: '/img/icon2.png',
iconSize: [38, 42],
})
const newUserMarker = L.marker([coords.lat, coords.lng], {
icon: icon
});
newUserMarker.bindPopup('Wear your Mask!');
map.addLayer(newUserMarker);
});
socket.on('userCoordinates', (coords) => {
console.log(coords);
socket.broadcast.emit('newUserCoordinates', coords);
});
map.addLayer(tile);
}
else{
setTimeout(setMap, 100);
}
}
setMap();
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.