<div id="balance">0.00</div>
<div id="sum">0.01</div>
<div id="result">000</div>
<div id="message">000</div>
<button>ROLL</button>
var x = 0;
$(document).ready(function(){
$("button").click(function () {
var num = setInterval(function () {
$("#result").text(Math.floor(Math.random() * 100))
}, 10);
setTimeout(function () {
clearInterval(num);
var sum = parseFloat($("#sum").html());
var fnal = $("#result").html();
if (fnal > 50) {
x += sum;
$("#balance").text(x.toFixed(2));
$("#message").text("Balance insufficient!");
}
else {
x -= sum;
$("#balance").text(x.toFixed(2));
$("#message").text("Balance insufficient!");
}
},500);
});
});
I don't know how to do this within (if) and (else) instruction.
So, please how can I do this? and I'll appreciate your efforts.
Please try to this code i hope helpfull thanks.
var x = 0;
$(document).ready(function(){
$("button").click(function () {
var num = setInterval(function () {
$("#result").text(Math.floor(Math.random() * 100))
}, 10);
setTimeout(function () {
clearInterval(num);
var sum = parseFloat($("#sum").html());
var fnal = $("#result").html();
if (true) {
x += sum;
$("#balance").text(x.toFixed(2));
$("#message").text(x === 0 ? "Balance insufficient!" : '');
}
else {
x -= sum;
$("#balance").text(x.toFixed(2));
$("#message").text(x === 0 ? "Balance insufficient!" : '');
}
},500);
});
});
I moved the balance and message out of the if/else to show a message when x === 0. Is this what youre looking for?
var x = 0;
$(document).ready(function() {
$("button").click(function() {
var num = setInterval(function() {
$("#result").text(Math.floor(Math.random() * 100));
}, 10);
setTimeout(function() {
clearInterval(num);
var sum = parseFloat($("#sum").html());
var fnal = parseFloat($("#result").html());
if (fnal > 50) {
x += sum;
} else {
x -= sum;
}
// Moved out of if/else, because it was the same
$("#balance").text(x.toFixed(2));
// If x === 0, show message, otherwise clear the text
$("#message").text(x === 0 ? "Balance insufficient!" : '');
}, 500);
});
});
Note: if you havent seen the <condition? ? <true> : <false> yet, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
Related
Here is my code below, it works fine but I need to change the countdown timer into minutes and seconds. Something like this 59:59 and 09:09 when it's below 10. And thanks so much for the help.
var test = 3600;
$('button').click(function(){
if (localStorage.getItem("counter")) {
if (localStorage.getItem("counter") <= 0) {
var value = test;
}
else {
var value = localStorage.getItem("counter");
}
}
else {
var value = test;
}
$('#divCounter').text(value);
var counter = function() {
if (value == 0) {
localStorage.setItem("counter", test);
value = 0;
}
else {
value = parseInt(value) - 1;
localStorage.setItem("counter", value);
}
$('#divCounter').text(value);
};
var interval = setInterval(function() { counter(); }, 1000);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="divCounter">3600</div>
<br>
<button>Start The Counter</button>
One minute = total / 60.
Seconds = total % 60.
Add 0 if minutes or seconds below 10.
Just add this calculations before adding to html.
var test = 3600;
$('button').click(function(){
if (localStorage.getItem("counter")) {
if (localStorage.getItem("counter") <= 0) {
var value = test;
}
else {
var value = localStorage.getItem("counter");
}
}
else {
var value = test;
}
$('#divCounter').text(value);
var counter = function() {
if (value == 0) {
localStorage.setItem("counter", test);
value = 0;
}
else {
value = parseInt(value) - 1;
localStorage.setItem("counter", value);
}
var minutes = Math.floor(value / 60)
var seconds = value % 60
if (minutes<10) minutes = '0'+minutes
if (seconds<10) seconds = '0'+seconds
$('#divCounter').text(minutes+':'+seconds);
};
var interval = setInterval(function() { counter(); }, 1000);
});
<div id="counter">100</div>
<script>
function myFunction() {
var person = prompt("Please enter One number (N) where (N) is a positive integer.", "");
if (person != null) {
document.getElementById("demo").innerHTML =
"Hello " + person + "! How are you today?";
}
}
function countdown() {
var i = document.getElementById('counter');
i.innerHTML = parseInt(i.innerHTML) - 1;
if (parseInt(i.innerHTML) == 95) {
clearInterval(timerId);
}
}
var timerId = setInterval(function () { countdown(); }, 1000);
</script>
<p id="demo"></p>
Example Test Cases:
Input: 10
Output:
100 98 96 94 92
99 97 95 93 91
Another one:
Input: 5
Output:
100 98 96
99 97
I have made my own script which counts down from 100-0, and places each number into an Array variable depending on whether it's Odd/Even.
Here it is:
var even = [];
var odd = [];
var number = 100;
changeNumber();
function changeNumber() {
document.getElementById("counter").innerText = number;
if (checkParity(number) == "even") {
console.log("even");
even.push(number);
} else {
console.log("odd");
odd.push(number);
}
if (number == 0) {
console.log("[Finished]");
console.log("Even numbers:");
console.log(even);
console.log("Odd numbers:");
console.log(odd);
} else {
setTimeout(function () {
number--;
changeNumber();
}, 100);
}
}
function checkParity(n) {
if (n % 2 == 0) {
return "even"
} else {
return "odd"
}
}
<div id="counter">100</div>
I hope that can help.
(function () {
const input = prompt("Please enter One number (N) where (N) is a positive integer.", "");
let count = 100;
if (isNaN(input) || input < 1) return;
const inerval = setInterval(() => {
count--;
if (count % 2 == 0) {
console.log('This is even Numbers ' + count);
} else {
console.log('This is odd Numbers ' + count);
}
if (input == count) return clearInterval(inerval);
}, 1000);
})();
I think this is a possible solution, without many changes to your original code:
<div id="counter">100</div>
<p id="results"></p>
<script>
var input = parseInt(prompt("Please enter One number (N) where (N) is a positive integer.", ""));
var half = parseInt(input / 2);
var i = document.getElementById('counter');
var res = document.getElementById('results');
res.innerHTML += "Even: ";
if (parseInt(i.innerHTML) % 2 == 0) {
res.innerHTML += i.innerHTML;
input--;
}
function countdown() {
if (input == 0) {
clearInterval(timerId);
return;
}
if (input == half) {
var newValue;
if (input % 2) {
newValue = parseInt(i.innerHTML) + input * 2 - 3;
}
else {
newValue = parseInt(i.innerHTML) + input * 2 - 1;
}
i.innerHTML = newValue;
res.innerHTML += "</br> Odd: ";
}
else {
i.innerHTML = parseInt(i.innerHTML) - 2;
}
res.innerHTML += " " + i.innerHTML;
input--;
}
var timerId = setInterval(function(){ countdown(); }, 1000);
</script>
<p id="demo"></p>
Basically you decrease it by 2 till you reach the half, then you go back to the start - 1, looping through the odd numbers (or even, if the counter value was odd at first).
Im running a recursive setTimeout function and I can run it many times, it has a clearTimeout, with this property I can handle how to stop the function running.
But I can't figure out how to stop it in another function.
var a = 0;
var b = 0;
function Listener(x, y) {
var lValue = y == true ? a : b;
if (lValue < 100) {
console.log(lValue);
if(y == true){
a+=x;
}else{
b+=x;
}
setTimeout(Listener.bind(this, x, y), 1000);
} else {
clearTimeout(Listener);
if(y == true){
a=0;
}else{
b=0;
}
}
}
When i tried to run it twice, it works:
My doubt is: How can I stop a particular running instance.
A couple notes:
Given the constant timeout of 1000, you should be using setInterval() instead. It will greatly simplify your function, and allow you to cancel the interval whenever you want.
Using global variables is code smell, create an object instead to hold a reference to the value being incremented. Doing so will also allow your function parameters to be more intuitive.
Here's a solution incorporating these two suggestions:
function range (step, start = 0, stop = 100) {
const state = {
value: start,
interval: setInterval(callback, 1000)
};
function callback () {
if (state.value < stop) {
console.log(state.value);
state.value += step;
} else {
console.log('timer done');
clearInterval(state.interval);
}
}
callback();
return state;
}
const timer1 = range(10);
const timer2 = range(20);
setTimeout(() => {
console.log('stopping timer 1');
clearInterval(timer1.interval);
}, 2500);
setTimeout(() => {
console.log('timer 2 value:', timer2.value);
}, 3500);
You could elevate the timer to a higher scope that is accessible by other functions.
var a = 0;
var b = 0;
var timer = null;
function Listener(x, y) {
var lValue = y == true ? a : b;
if (lValue < 100) {
console.log(lValue);
if (y == true) {
a += x;
} else {
b += x;
}
timer = setTimeout(Listener.bind(this, x, y), 1000);
} else {
clearTimeout(timer);
if (y == true) {
a = 0;
} else {
b = 0;
}
}
}
function clearTimer() {
if (timer !== null) clearTimeout(timer);
}
Listener(3, 3);
// clear the timer after 3 secnods
setTimeout(clearTimer, 3000);
create a variable and store the reference of setTimout on that variable, after that you just clearTimout with that variable reference
e.x
GLOBAL VARIABLE:
var k = setTimeout(() => { alert(1)}, 10000)
clearTimeout(k)
var a = 0;
var b = 0;
var recursiveFunctionTimeout = null;
function Listener(x, y) {
var lValue = y == true ? a : b;
if (lValue < 100) {
console.log(lValue);
if(y == true){
a+=x;
}else{
b+=x;
}
recursiveFunctionTimeout = setTimeout(Listener.bind(this, x, y), 10);
} else {
clearTimeout(recursiveFunctionTimeout);
if(y == true){
a=0;
}else{
b=0;
}
}
}
LOCAL VARIABLE:
var a = 0;
var b = 0;
function Listener(x, y) {
var lValue = y == true ? a : b;
var recursiveFunctionTimeout = null;
if (lValue < 100) {
console.log(lValue);
if(y == true){
a+=x;
}else{
b+=x;
}
recursiveFunctionTimeout = setTimeout(Listener.bind(this, x, y), 10);
} else {
clearTimeout(recursiveFunctionTimeout);
if(y == true){
a=0;
}else{
b=0;
}
}
}
My goal is, if a page contains the specified button, click it, and increase the amt_clicked by 1. When amt_clicked is greater than 15, wait for 60 seconds and reset amt_clicked. I have no idea how do this if statement. Example:
var amt_clicked = 0;
while (1) {
while (amt_clicked < 15) {
if (button found) { // this is where I am lost
iimPlay("TAG POS={{amt_clicked}} TYPE=BUTTON ATTR=TXT:Get");
amt_clicked++;
}
}
iimPlay("WAIT SECONDS=60");
amt_clicked = 0;
}
This will run 20 times per second, using the window.setInterval() function:
var amt_clicked = 0;
var amt_cooldown = 1200;
setInterval(function(){
if (amt_cooldown === 0)
amt_cooldown = 1200;
else if (amt_cooldown < 1200)
amt_cooldown -= 1;
else if (amt_clicked > 15) {
amt_clicked = 1;
amt_cooldown -= 1;
} else {
amt_clicked -= 1;
//Click
}, 50);
You can use combination of setInterval and setTimeout.
I have added comments to code for you to understand.
var amt_clicked = 0;
var setTimeoutInProcess = false;
//processing the interval click function
setInterval(() => {
checkButtonAgain();
}, 200);
function checkButtonAgain() {
var element = document.getElementById('iNeedtoBeClicked');
//if clicked 15 times then need to wait for 60 seconds
if (amt_clicked === 15) {
if (!setTimeoutInProcess) {
setTimeoutInProcess = true;
setTimeout(function() {
//resetting the amt-clicked
amt_clicked = 0;
setTimeoutInProcess = false;
}, 60000);
} else {
console.log('waiting');
}
} else if (typeof(element) != 'undefined' && element != null) {
//triggering click and increasing the amt_clicked
element.click();
amt_clicked++;
}
console.log(amt_clicked);
}
<button id="iNeedtoBeClicked">Click ME Button</button>
How to make a script repeat it self faster than a setTimeout allows but still not as fast as possible?
Check this demo for 2 examples.
(I post the demo code under also)
var x = 0;
var divEl = document.getElementById('counter');
var divEl2 = document.getElementById('counter2');
document.getElementById('gosettimeout').addEventListener('click', go, false);
document.getElementById('gotoofast').addEventListener('click', go2, false);
function go() {
x++;
divEl.innerHTML = x;
if (x > 100) {
return false;
}
setTimeout(function () {
go();
}, 0);
}
function go2() {
x++;
divEl2.innerHTML = x;
if (x > 100) {
return false;
}
go2();
}
var x = 0;
var divEl = document.getElementById('counter');
var divEl2 = document.getElementById('counter2');
document.getElementById('gosettimeout').addEventListener('click', go, false);
document.getElementById('gotoofast').addEventListener('click', go2, false);
function go() {
x++;
divEl.innerHTML = x;
if (x > 100) {
return false;
}
if (x % 2 == 0) {
setTimeout(function () {
go();
}, 0);
} else {
go();
}
}
function go2() {
x++;
divEl2.innerHTML = x;
if (x > 100) {
return false;
}
go2();
}
Two times faster, but not as fast as possible =)
var x = 0;
var divEl = document.getElementById('counter');
var divEl2 = document.getElementById('counter2');
document.getElementById('gosettimeout').addEventListener('click', go, false);
document.getElementById('gotoofast').addEventListener('click', go2, false);
function go() {
divEl.innerHTML = ++x;
if (x > 100) {
return false;
}
if (x % 5 == 0) {
setTimeout(function () {
go();
}, 0);
} else {
go();
}
}
function go2() {
x++;
divEl2.innerHTML = x;
if (x > 100) {
return false;
}
go2();
}