After page load function "intro" is launch. This function displays two buttons, after click on one of them another function "startGame" starts with parameter "innerHTML" from click button.
Then the right panel appears and "START" button counts time from 10. Countdown stops when user click on map or time reaches 0.
After "START" user can click on map (to add a marker) only ones, and click "START" again to add another marker.
When users click on map 4 times "game" finishes and two buttons appear again.
And that is when a problem starts. When function "startGame" starts again and user clicks "START" button, countdown doubles (you can see it in console).
If user clicks on map one countdown stops but second still counts to zero.
Can anyone tell me why time is doubled?
Link to live version: http://najlepszekomisy.co.pl/
Thank you.
var elem =
{
map: document.getElementById("mapa"),
panel: document.getElementById("right_panel"),
games: document.getElementById("games"),
draw: document.getElementById("draw"),
points: document.getElementById("pointsGet"),
timer: document.getElementById("timer")
};
(function intro()
{
elem.games.addEventListener("click",function(e){
if(e.target.tagName==="H4")
{
TweenMax.to(elem.games,1,{ease: Back.easeOut,top: -50,onComplete:function(){
startGame(e.target.innerHTML);}})
}
},false)
})();
function startGame(hit)
{
var gameElement =
{
mapa:0,
clickListener:0,
number:0,
usingSet:4,
timeNum:10
};
(function loadMap()
{
var mapOptions =
{
zoom: 7,
disableDefaultUI: true,
zoomControl: true,
center: new google.maps.LatLng(51.95442214470796, 19.14093017578125)
};
gameElement.mapa = new google.maps.Map(elem.map, mapOptions);
google.maps.event.addListener(gameElement.mapa,'idle',function()
{
TweenMax.to("#right_panel",2,{ease: Back.easeIn,right: 0,onComplete:function(){
TweenMax.set(".anim_from_bottom", { display: 'block' });
TweenMax.staggerFrom(".anim_from_bottom",0.5,{y:1600},0.2);
google.maps.event.clearListeners(gameElement.mapa, 'idle');
}});
});
})();
elem.draw.addEventListener("click",function(){
if(gameElement.number<gameElement.usingSet)
{
gameElement.number++;
timer.time=gameElement.timeNum;
timer.calcTime();
gameElement.clickListener = google.maps.event.addListener(gameElement.mapa, "click", function (e) {
addMarker(e.latLng.lng(), e.latLng.lat());
});
elem.draw.disabled=true;
}else{result()}},false);
function addMarker(lng,lat)
{
timer.stopTime();
var opcjeMarkera =
{
position: new google.maps.LatLng(lat,lng),
map: gameElement.mapa,
title: hit
};
var marker = new google.maps.Marker(opcjeMarkera);
google.maps.event.removeListener(gameElement.clickListener);
elem.draw.disabled=false;
}
var timer =
{
time: 0,
startTime:0,
calcTime:function()
{
elem.timer.className = "elem";
elem.timer.innerHTML = timer.time;
console.log(timer.time);
if(timer.time===0){elem.timer.className = " ";clearTimeout(timer.startTime);}
else
{
--timer.time;
timer.startTime = setTimeout(timer.calcTime, 1000);
}
},
stopTime:function()
{
clearTimeout(timer.startTime);
elem.timer.className = " ";
this.time=gameElement.timeNum;
}
};
function result ()
{
console.log("koniec");
gameElement.number=0;
TweenMax.to("#right_panel",2,{ease: Back.easeIn,right: -300});
TweenMax.to(elem.games,1,{ease: Back.easeOut,top: 50})
}
}
Every time your H4 button is clicked it calls startGame function. Every time startGame function is called it adds one more event listener to the Start button, so start handler is called once when you play first time, twice the second time, thrice next time etc.
To avoid this you can:
add event listener in init function (which is called once)
remove event listeners before adding new one (see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener)
use onclick property instead of .addEventListener() method if there is only one handler needed for this button
You need to make sure that the calcTime function which includes this code:
timer.startTime = setTimeout(timer.calcTime, 1000);
cannot be invoked while it is already running, because if it does, then another call to calcTime will be queued up causing the clock to tick twice as fast.
Related
I think I'm missing something basic about javascript. I could use your help!
Scenario: After clicking "search" the google map loads. As it loads, a checkbox appears that says, "Redo results when map is moved" (similar to yelp). If the map is dragged or zoomed, the results update. That part works fine.
Problem: The value of the checkbox (true or false) is recognized by the redo() function when the map loads, and it doesn't check it again after that (because even though redo() is updating the results it's not reloading the map). So if you toggle the checkbox, the "redo" function doesn't notice!
Simplified Code:
//HAML
%input{:type => "checkbox", :id => "followCheck", :name => "followCheck", :onclick => 'handleClick();'}
//load the map
function showLocations() {
map = new google.maps.Map(document.getElementById("map_canvas"), {
bunch of map stuff;
map.fitBounds(bounds);
redo(); //this is the redo function, see below
}
}
function handleClick(checkbox) {
var chk = document.getElementById("followCheck").checked;
console.log("checkbox value is currently " + chk);
return chk;
}
function redo() {
var chk = handleClick();
console.log("inside redo, value is " + chk);
if (chk == true) {
google.maps.event.addListener(map, 'mouseup', function() {
mapAdjusted = 1;
updateMapAfterUserInteraction();
});
google.maps.event.addListener(map, 'zoom_changed', function() {
mapAdjusted = 1;
updateMapAfterUserInteraction();
});
}
}
(note, that redo() function was written by my collaborator, who is smarter than me and currently out of town.)
So, Redo() loads when the map loads. If the checkbox is true when the map loads, then the redo() function processes zoom/drag. But it still does it even after changing the value to false (and if the value is false when the page loads, the opposite happens).
What is needed to get redo() to see that the value has changed? push()? bind()? get/set? change()? addEventListener()? Should I reload the map when the checkbox is toggled (I'd rather not)? A whole new approach? I'm stuck! Thanks. I'm open to JQuery ideas, too.
I solved this. First off, with a small adjustment to the function I was able get it to turn ON after checking the box (even if the results loaded while the box was unchecked via a previous page session). Here is that code (I actually found like three variations that all had the same result):
function redo() {
if ($("#followCheck").is(':checked')) {
google.maps.event.addListener(map, 'mouseup', function() {
mapAdjusted = 1;
updateMapAfterUserInteraction();
});
google.maps.event.addListener(map, 'zoom_changed', function() {
mapAdjusted = 1;
updateMapAfterUserInteraction();
});
}
}
The problem was that it didn't turn off. And that's because I've added two listeners! This answer helped me remove the listeners. Note that (for me) it only worked when the removeListener() function is within the addListener() function. Kind of strange.
function redo() {
var listener1 = google.maps.event.addListener(map, 'mouseup', function() {
if (!$("#followCheck").is(':checked')) {
google.maps.event.removeListener(listener1);
} else {
mapAdjusted = 1;
updateMapAfterUserInteraction();
}
});
var listener2 = google.maps.event.addListener(map, 'zoom_changed', function() {
if (!$("#followCheck").is(':checked')) {
google.maps.event.removeListener(listener2);
} else {
mapAdjusted = 1;
updateMapAfterUserInteraction();
}
});
}
Oh and I also changed onclick() to onchange()
I have a setInterval on a function X that runs every 500ms. In this function X, I call another function Y that essentially binds an event on some divs. However, I would like to unbind these events the next time the function X is called (to start "fresh"). My code doesn't seem to work:
setInterval(this.board.updateBoard, 500); //called from another constructor
This then initiates the functions below:
Board.prototype.updateBoard = function() {
//I attempt to unbind ALL my divs
var divs = this.$el.find("div");
for(var i = 0; i < divs.length; i++) {
$(divs[i]).unbind(); //Apparently this doesn't work?
}
//...some code here...
//find appropriate $div's (multiple of them), and then calls this.beginWalking() below on each of those
//loop here
this.beginWalking($div, direction + "0", direction + "1");
//end of loop
}
//alternate between classes to give appearance of walking
Board.prototype.beginWalking = function ($div, dir0, dir1) {
return setInterval(function () {
if ($div.hasClass(dir0)) {
$div.removeClass(dir0);
$div.addClass(dir1);
} else {
$div.removeClass(dir1);
$div.addClass(dir0);
}
}.bind(this), 80);
};
Basically, updateBoard is called every 500ms. Each time it's called, beginWalking is called to set another interval on a div. The purpose of this other interval, which functions correctly, is to add and remove a class every 80ms. I just can't seem to unbind everything before the next updateBoard is called.
Any suggestions appreciated!
use clearInterval()
edit: $(selector).toggleClass(dir0) might also be helpful
// In other file, use a global (no var) if you need to read it from another file:
updaterGlobal = setInterval(this.board.updateBoard, 500);
// store interval references for clearing:
var updaterLocals = [];
Board.prototype.updateBoard = function() {
//I attempt to unbind ALL my divs
var divs = this.$el.find("div");
// Stop existing div timers:
while(updaterLocals.length > 0){
clearInterval(updaterLocals[0]);
updaterLocals.shift(); // remove the first timer
}
//...some code here...
//loop here to call the below on several $div's
this.beginWalking($div, direction + "0", direction + "1");
//end of loop
}
//alternate between classes to give appearance of walking
Board.prototype.beginWalking = function ($div, dir0, dir1) {
var interval = setInterval(function () {
if ($div.hasClass(dir0)) {
$div.removeClass(dir0);
$div.addClass(dir1);
} else {
$div.removeClass(dir1);
$div.addClass(dir0);
}
}.bind(this), 80);
// Save the timer:
updaterLocals.push(interval);
return;
};
I have a question concerning clicks on a map in leaflet. If I click on the map I want to set a marker there, but if doubleclick on the map I just want to zoom in without setting a marker. So I have the follwing code:
var map = L.map(attrs.id, {
center: [scope.lat, scope.lng],
zoom: 14
});
var marker = L.marker([scope.lat, scope.lng],{draggable: true});
map.on('click', function(event){
marker.setLatLng(event.latlng);
marker.addTo(map);
});
The problem now is, when I doublclick on the map the click event is also fired and I would like to remove that behavior. How can I achieve that?
Thanks
Magda
So, I found a way to do that, I am still not sure, if there is a better way to do it.
var map = L.map(attrs.id, {
center: [scope.lat, scope.lng],
zoom: 14
});
map.clicked = 0;
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18
}).addTo(map);
var marker = L.marker([scope.lat, scope.lng],{draggable: true});
map.on('click', function(event){
map.clicked = map.clicked + 1;
setTimeout(function(){
if(map.clicked == 1){
marker.setLatLng(event.latlng);
marker.addTo(map);
map.clicked = 0;
}
}, 300);
});
map.on('dblclick', function(event){
map.clicked = 0;
map.zoomIn();
});
Had the same problem of 2 x unwanted click events firing when listening for dblclick.
Note: I wanted single and double clicks on the same element to perform different actions.
I adapted this approach to my leaflet map, which is not bullet proof but eliminates 99% of the conflicts:
var timer = 0;
var delay = 200;
var prevent = false;
$("#target")
.on("click", function() {
timer = setTimeout(function() {
if (!prevent) {
doClickAction();
}
prevent = false;
}, delay);
})
.on("dblclick", function() {
clearTimeout(timer);
prevent = true;
doDoubleClickAction();
});
Credit: CSS-Tricks
Code Pen Example
This is still an issue on recent (leaflet 1.4) versions.
Alternative approach I used that:
legit usage of setTimeout and clearTimeout
without adding random props to the map object
no jQuery:
map.on('click', function(event) {
if (_dblClickTimer !== null) {
return;
}
_dblClickTimer = setTimeout(() => {
// real 'click' event handler here
_dblClickTimer = null;
}, 200);
})
.on("dblclick", function() {
clearTimeout(_dblClickTimer);
_dblClickTimer = null;
// real 'dblclick' handler here (if any). Do not add anything to just have the default zoom behavior
});
Note that the 200 ms delay must be tested. On my environment using a value like 100 was not enough as the doubleclick event is triggered with a delay.
I have added a text slideshow to a div that is called by JQuery on hover. How do I get the slideshow to start from the beginning each time the user hovers on the div? Right now it just continues to loop after the first time it is activated.
Thanks in advance!
<script>
$(document).ready(function(){
$('#mercury .infos').hover(
function () {
if($("a.active").is('.mercury')){
$("#descriptionls").fadeIn("2000");
};
var quotes = [
"Who’s the one who’s always there when that keeps happening?",
"Learn to dismantle self-defeating behaviors",
"JOIN THE FLOW TODAY",
];
var i = 0;
setInterval(function() {
$("#lstextslide").html(quotes[i]);
if (i == quotes.length)
i=0;
else
i++;
}, 1 * 4000);
});
});
</script>
You have to cancel the previous interval and call the function that updates the HTML immediately. See http://jsfiddle.net/xozL96fj/
$(document).ready(function(){
var intervalTimer = null;
$('#mercury .infos').hover(
function () {
if($("a.active").is('.mercury')){
$("#descriptionls").fadeIn("2000");
}
if (intervalTimer !== null) {
clearInterval(intervalTimer);
}
var quotes = [
"Who’s the one who’s always there when that keeps happening?",
"Learn to dismantle self-defeating behaviors",
"JOIN THE FLOW TODAY",
];
var i = 0;
function update() {
$("#lstextslide").html(quotes[i]);
i = (i + 1) % quotes.length;
}
// Call it immediately, don't wait until the interval
update();
intervalTimer = setInterval(update, 4000);
});
});
You need to make a check if your slider has already been activated. If you hover over your slider when the slider is already active, you will have to reset $i, and call setInterval() again.
I have an element on my page that I need to attach onclick and ondblclick event handlers to. When a single click happens, it should do something different than a double-click. When I first started trying to make this work, my head started spinning. Obviously, onclick will always fire when you double-click. So I tried using a timeout-based structure like this...
window.onload = function() {
var timer;
var el = document.getElementById('testButton');
el.onclick = function() {
timer = setTimeout(function() { alert('Single'); }, 150);
}
el.ondblclick = function() {
clearTimeout(timer);
alert('Double');
}
}
But I got inconsistent results (using IE8). It would work properly alot of times, but sometimes I would get the "Single" alert two times.
Has anybody done this before? Is there a more effective way?
Like Matt, I had a much better experience when I increased the timeout value slightly. Also, to mitigate the problem of single click firing twice (which I was unable to reproduce with the higher timer anyway), I added a line to the single click handler:
el.onclick = function() {
if (timer) clearTimeout(timer);
timer = setTimeout(function() { alert('Single'); }, 250);
}
This way, if click is already set to fire, it will clear itself to avoid duplicate 'Single' alerts.
If you're getting 2 alerts, it would seem your threshold for detecing a double click is too small. Try increasing 150 to 300ms.
Also - I'm not sure that you are guaranteed the order in which click and dblclick are fired. So, when your dblclick gets fired, it clears out the first click event, but if it fires before the second 'click' event, this second event will still fire on its own, and you'll end up with both a double click event firing and a single click event firing.
I see two possible solutions to this potential problem:
1) Set another timeout for actually firing the double-click event. Mark in your code that the double click event is about to fire. Then, when the 2nd 'single click' event fires, it can check on this state, and say "oops, dbl click pending, so I'll do nothing"
2) The second option is to swap your target functions out based on click events. It might look something like this:
window.onload = function() {
var timer;
var el = document.getElementById('testButton');
var firing = false;
var singleClick = function(){
alert('Single');
};
var doubleClick = function(){
alert('Double');
};
var firingFunc = singleClick;
el.onclick = function() {
// Detect the 2nd single click event, so we can stop it
if(firing)
return;
firing = true;
timer = setTimeout(function() {
firingFunc();
// Always revert back to singleClick firing function
firingFunc = singleClick;
firing = false;
}, 150);
}
el.ondblclick = function() {
firingFunc = doubleClick;
// Now, when the original timeout of your single click finishes,
// firingFunc will be pointing to your doubleClick handler
}
}
Basically what is happening here is you let the original timeout you set continue. It will always call firingFunc(); The only thing that changes is what firingFunc() is actually pointing to. Once the double click is detected, it sets it to doubleClick. And then we always revert back to singleClick once the timeout expires.
We also have a "firing" variable in there so we know to intercept the 2nd single click event.
Another alternative is to ignore dblclick events entirely, and just detect it with the single clicks and the timer:
window.onload = function() {
var timer;
var el = document.getElementById('testButton');
var firing = false;
var singleClick = function(){
alert('Single');
};
var doubleClick = function(){
alert('Double');
};
var firingFunc = singleClick;
el.onclick = function() {
// Detect the 2nd single click event, so we can set it to doubleClick
if(firing){
firingFunc = doubleClick;
return;
}
firing = true;
timer = setTimeout(function() {
firingFunc();
// Always revert back to singleClick firing function
firingFunc = singleClick;
firing = false;
}, 150);
}
}
This is untested :)
Simple:
obj.onclick=function(e){
if(obj.timerID){
clearTimeout(obj.timerID);
obj.timerID=null;
console.log("double")
}
else{
obj.timerID=setTimeout(function(){
obj.timerID=null;
console.log("single")
},250)}
}//onclick
Small fix
if(typeof dbtimer != "undefined"){
dbclearTimeout(timer);
timer = undefined;
//double click
}else{
dbtimer = setTimeout(function() {
dbtimer = undefined;
//single click
}, 250);
}
, cellclick :
function(){
setTimeout(function(){
if (this.dblclickchk) return;
setTimeout(function(){
click event......
},100);
},500);
}
, celldblclick :
function(){
setTimeout(function(){
this.dblclickchk = true;
setTimeout(function(){
dblclick event.....
},100);
setTimeout(function(){
this.dblclickchk = false;
},3000);
},1);
}
I found by accident that this works (it's a case with Bing Maps):
pushpin.clickTimer = -1;
Microsoft.Maps.Events.addHandler(pushpin, 'click', (pushpin) {
return function () {
if (pushpin.clickTimer == -1) {
pushpin.clickTimer = setTimeout((function (pushpin) {
return function () {
alert('Single Clic!');
pushpin.clickTimer = -1;
// single click handle code here
}
}(pushpin)), 300);
}
}
}(pushpin)));
Microsoft.Maps.Events.addHandler(pushpin, 'dblclick', (function (pushpin) {
return function () {
alert('Double Click!');
clearTimeout(pushpin.clickTimer);
pushpin.clickTimer = -1;
// double click handle here
}
}(pushpin)));
It looks like the click event masks the dblclick event, and this usage is clearing it when we add a timeout. So, hopefully, this will work also with non Bing Maps cases, after a slight adaptation, but I didn't try it.