I'm learning javascript and I'm having a very hard time to learn the logic behind. I'm trying to make a pomodoro timer but I can't figure out how to change the time when I change the button tab to "break". I've been trying to do this for about 5/6 days now and I'm running out of ideas and my logic just don't work.
In my last attempt I created a function to see if the break button element had an "active" class and change the time if it did but it didn't work.
function changeTimeBreak() {
const breakTab = document.getElementsByClassName('break-tab')
if (breakTab.hasClass('active')) {
minutes = 10
updateCounterEl()
return
}
}
You are using JQuery's .hasClass method. What you should use is Javascript's element.classList .contains method.
I've edited your function. This should update the clock when break-tab button is active.
function changeTimeBreak() {
let breakTab = document.getElementsByClassName('break-tab');
if (breakTab[0].classList.contains('active')) {
minutes = 10;
updateCounterEl();
}
}
I have a problem. I am making an addon for Chrome and I want to check a value each time the timer is at 15 sec in an external website.
I have got this, for example:
<div id="timer">17.38</div>
or
<div id="timer">7.16</div>
I need to activate an event when my div is:
<div id="timer">15.00</div>
I need an event execute function in Javascript.
I need your help please
you can use this :
setInterval(function(){
check_timer();
}
,100)
the check function
function check_timer(){
var obj = document.getElementById("timer");
if (obj.innerHTML == "15.00"){
//your code here
}
}
I have a HTML page which has CSS animations and I wanted to know if there is a way where I wanted the page to reload 3 times and then stop it. Also when reloading it 3 times, for each time I want it to have a delay so for example the first time when my page loads it will play the animations and then after all the animations have been played I want it to reload again so some kind of a delay function?
Thank you.
Add this on page load.
<script>
var timeinmilliseconds = 3000;
var reloadCnt = window.sessionStorage.getItem( "reloadCounter") ? parseInt(window.sessionStorage.getItem( "reloadCounter")) + 1 : 1;
window.sessionStorage.setItem( "reloadCounter", reloadCnt )
console.log(reloadCnt);
if ( reloadCnt <= 3 )
setTimeout(function(){ window.location.reload(true) }, timeinmilliseconds);
</script>
Let me know if this helps. This is pure javascript, just add it to your page and let me know. Here is the link http://jsfiddle.net/
I'm currently preparing a cool video presentation on my html web page. At the end of it, I want to be able to click on the video and be taken to a page - however I only want the link to come into effect at a certain time.
I've done some research and I can't find anything about this.
As an example, let's say that I want to make a link on something...
This link will go somewhere after 15 seconds
How can I make it so that <a> tag doesn't work for 15 seconds with jQuery or JavaScript? (JavaScript preferred but it doesn't really matter!). Remember - I don't want that whole line of code to suddenly appear - prior to the link working that should just be text!
Thanks!
Here delay is set to 3 seconds (3000 milliseconds in call to setTimeout). Change it to 15000 to make it 15 seconds
$(document).ready(function() {
setTimeout(convertTextToLink, 3000);
});
function convertTextToLink() {
$('#thanks').html('Thanks for watching. You may now proceed.');
}
Html
<h1>Hello</h1>
<p>here are your vids</p>
<div id="thanks">Thanks for watching</div>
var waiting = true;
//set waiting to false after 15 seconds
setTimeOut(function() { waiting = false },15000);
$('#automate').click(function(e) {
if(waiting === true) {
e.preventDefault(); //prevent the link from firing
}
});
Can anyone point me to an example of code for a page that begins to automatically scroll when the user is idle for an amount of time? I think this is slightly beyond my skill set. I think JQuery or something similar might be appropriate but I just can't seem to figure it out. I'm designing a site for the nonprofit I work for and we don't have the money to hire a programmer. I wouldn't ask anyone to code anything for me, just to point me in the right direction. Thank you so much.
Julie K.
Here is something quick and dirty that will do what you want. I currently have it set to 2 seconds idle time, but you can change that as you wish.
var now = new Date();
setInterval(function(){
var nnow = new Date();
if(nnow.getTime() - now.getTime() >= 2000)
$('body').animate({scrollTop: '+=50'}, 2000, 'linear');
}, 2000);
$(document)
.mousemove(function(){ now = new Date(); $('body').stop(); })
.keypress(function(){ now = new Date(); $('body').stop(); });
Edit: added .stop in mousemove and keypress events to stop scrolling immediately when user moves mouse or presses a key, rather than waiting for animation to complete.
jQuery would definitely help here.
You should handle the keyboard and mouse events to catch user activity.
Then, whenever you see activity, call setTimeout to make your code run 2 minutes after the activity. Save the return value of the setTimeout call in a global variable, and call clearTimeout on it before setTimeout to clear the last timeout.
For example: (Using jQuery and the scrollTo plugin)
var timeout = false;
$(document.body).bind('keydown keyup mousemove mouseup', function() {
if (timeout) clearTimeout(timeout);
timeout = setTimeout(userIsIdle, 120000); //120,000 milliseconds
});
function userIsIdle() {
$(document.body).scrollTo('100%', 100000);
}
you could use a plugin like this, and set it to use JavaScripts setTimeout() function to trigger it, which could reset every time you detect certain user actions, such as keyDown and Click.
First you have to define what you mean by IDLE, I will assume that the user is not moving mouse for x amount of time.
Here are the steps pseudo js.
var lastTime=0;
var threshold=60000 ; // 1min
var howOftenToCheck = 1000;//1 sec
var inter = 0;
inter = setInterval(function() {
var delta=lastTime-currentTime;
if(delta>threashold){
clearInterval(inter);
window.scrollTo(xpos,ypos);
}
}, howOftenToCheck);
This should give you a general idea.
use the following jQuery functions to scroll the page's body
// Scrolling Down
$('body').animate({
scrollTop: '-=300px'
}, 2000);
// Scrolling Up
$('body').animate({
scrollTop: '+=300px'
}, 2000);
I think onBlur event is what you're looking for, so you can do like this :
<body onblur="$('html, body').animate({scrollTop:0}, 'slow');">
You can change the 0 to the vertical position where you want to scroll to.
Note: this will fire the scrolling when you go to other page or tab or where you can't see the page.