Issue
I have this code where I want to enable the amurFunc()
to be executed every 5 seconds.
The execution is a listener that listens if the user clicked on the div. He can actually click it every 5 seconds. Can you help me figure out where did I do wrong.
The code:
<div id=amur>
</div>
<script>
document.getElementById("amur").addEventListener('click', amurFunc());
function amurFunc() {
var funcTimer = 0;
while (funcTimer == 0) {
window.open('http://google.com','_blank');
funcTimer = 5;
while (funcTimer > 0) {
setInterval(
function () {
funcTimer--;
}, 1000;
);
}
}
}
</script>
Solution
Try this
<div id="amur" style="background-color:red">
<p>Test</p>
</div>
var el = document.getElementById('amur');
el.addEventListener('click',amurFunc);
var clicked = 0;
var d = new Date();
var s = d.getTime();
//console.log(s);
function amurFunc(){
if(clicked==0){
window.open('http://google.com', '_blank');
clicked++;
}else{
var clickedDate = new Date();
var clickedSecond = clickedDate.getTime();
//console.log(clickedSecond-s);
if((clickedSecond-s)>=5000){
window.open('http://google.com', '_blank');
s = clickedSecond;
}
}
}
Answered By - ErcanE
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.