Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I've been tryin for a while to combine 2 functions in my code but I cant get it to work properly. I hope someone can help me out...

1st

JavaScript
function formatTime(seconds) {
    var h = Math.floor(seconds / 3600),
        m = Math.floor(seconds / 60) % 60,
        s = seconds % 60;
    if (h < 10) h = "0" + h;
    if (m < 10) m = "0" + m;
    if (s < 10) s = "0" + s;
    return h + ":" + m + ":" + s;
}
var count = 5;
var counter = setInterval(timer, 1000);

function timer() {
   count--;
   if (count < 0) return clearInterval(counter);
  document.getElementById('timer').innerHTML = formatTime(count);
}  


2nd

JavaScript
$(document).ready(function(){
$("button").click(function () {
    $(".verwijder").eq(0).remove();
});
});


The first piece of code is a timer. When the timer is 0 or 00:00:00 I want the 2nd piece of code to happend which will result in deleting a list item.

Below you see the list that will have its first item removed when the timer is over

HTML
<div id="vakjesdiv">
	<ul>
		<li class="verwijder"><img class="aanbieding" src="images/jack.png"> <span id="timer"></span></li>
		<li class="verwijder"><img class="aanbieding" src="images/kpn.png">	<span id="timer2"></li>
		<li class="verwijder"><img class="aanbieding" src="images/mac.png"></li>
		<li class="verwijder"><img class="aanbieding" src="images/pathe.png"></li>
	</ul>
</div>


I've a live demo here http://tinyurl.com/nam3hkr[^]

If you click on the button on the top right you will see the effect I want to create.
Posted
Updated 14-Oct-14 2:02am
v3
Comments
[no name] 14-Oct-14 7:54am    
Why do you want to combine?? What role will the timer play in removing one of the html tag??
Member 11140766 14-Oct-14 7:59am    
Well, Iam creating a page for discounts (just for practice). The discounts timer is counting down as you can see on the demo. When the time is over the discount should be removed.

Later I want to add a timer to all 4 of them and when one is removed a new one gets added. But thats later :p
[no name] 14-Oct-14 8:06am    
then the place where you are checking the time, i.e. when the time is zero put the
$("button").click(function () {
$(".verwijder").eq(0).remove();
});
there. This would work out I guess!
Member 11140766 14-Oct-14 8:23am    
function timer() {
count--;
if (count < 0) return clearInterval(counter);
document.getElementById('timer').innerHTML = formatTime(count);
$(".verwijder").eq(0).remove();
}

You mean like this? This isnt working, Now it will remove the list immediately without waiting for the time to be at 0
[no name] 14-Oct-14 8:27am    
Is that peice of statement executed inside the If statement!!!

1 solution

C#
function timer() {
count--;
if (count < 0) {
$(".verwijder").eq(0).remove();
return clearInterval(counter);
document.getElementById('timer').innerHTML = formatTime(count);
}
}


Try like this please.
 
Share this answer
 
Comments
Member 11140766 14-Oct-14 9:35am    
Now we have to fix the timer comes back:p
[no name] 14-Oct-14 9:38am    
sure..:)
[no name] 14-Oct-14 9:39am    
Increase the timer time and then check if the timer is coming in the console html section.
Member 11140766 14-Oct-14 9:50am    
Nop...

http://i58.tinypic.com/343g5c5.png
[no name] 14-Oct-14 9:52am    
That means the overlay for timer or a span whatever you have used isnot coming to the view itself..
What is the code for getting the Timer!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900