How to make buttons blinking with specific time? - javascript

i would like to ask all , i am face with issue that i want to blink three html links when user clicks on the button with specific time
<a id="tip1">blink1</a>
<a id="tip2">blink2</a>
<a id="tip3">blink3</a>
<input type="button" value="CLICK ME" id="btn" />
$("#btn").click(function () {
doBlink(900);
});
function doBlink(900)
{
set blink for $("#tip1") is 300, after finishing for blinking one, we will call for blink2..etc..
}
Thanks in advance

Here is a Fiddle Demo that will do this:
$("#btn").click(function () {
doBlink(900);
});
function doBlink(value) {
var time = value/3;
blink(1, time);
}
function blink(id, time) {
var counter = 0;
var interval = setInterval(function () {
$("#tip" + id).toggleClass('blinked');
counter += 50;
if (counter >= time && id < 4) {
clearInterval(interval);
id++;
blink(id, time);
}
}, 100);
}

Related

how to run click event 10 times

My Code
window.onload = function(){
setInterval(function(){
if (typeof document.getElementsByClassName('btn-primary')[0]!="undefined"){
document.getElementsByClassName('btn-primary')[0].click();
}
}, 1000);
}
<button type="button" class="btn-primary" >Auto click</button>
i want to click button as class="btn-primary" only 10 time. but i won't find the code for it. my code repeats clicking every 1s.
Now my question is, how can i click the button only 10 times?
If you are using setInterval function, that function will trigger each time on the specified interval, you have to use clearInterval once the limit is reached.
function clickme() {
console.log('You clicked me');
}
let intervalFunction;
let count = 0;
window.onload = function () {
intervalFunction = setInterval(function () {
if (typeof document.getElementsByClassName('btn-primary')[0] != "undefined") {
document.getElementsByClassName('btn-primary')[0].click();
count++;
if (count === 10) {
console.log("Now you have clicked me 10 times!! let me take a break")
clearInterval(intervalFunction)
}
}
}, 1000);
}
<button class="btn-primary" onclick="clickme()">Click me</button>
If you dont need to have the interval between each click, you can direcly click it with a loop.
Example
function clickme() {
console.log('You clicked me');
}
let intervalFunction;
let count = 0;
window.onload = function () {
while(count < 10) {
if (typeof document.getElementsByClassName('btn-primary')[0] != "undefined") {
document.getElementsByClassName('btn-primary')[0].click();
count++;
}
}
}
<button class="btn-primary" onclick="clickme()">Click me</button>

How to make an auto clicker in JavaScript?

I'm currently making a clicker game and I wanted to know how to make a button that when I click on it, a number slowly goes up with intervals. I don't really know how to do the intervals and I've tried doing loops but loops are instant and have no intervals.
var number = 0;
function increase() {
for (var i = 0; i < 1; i++) {
document.getElementById("number").innerHTML = number += 1
}
}
<button onclick="increase()">
by 1
</button>
<div>
number:
<div id="number">
0
</div>
</div>
Can someone turn this into an auto clicker? as said above, i just want to click on the button once and have the value of the variable "number" go up. Thanks.
Here is mine showing how to
use an interval
use eventListener (unobtrusive coding)
clear the interval
how to increment a number and change speed
how to use data attributes
how to use classes and querySelector
var number = 0,
increment = 1,
speed=1000, // one second
tId;
function inc() {
clearInterval(tId); // stop anything already running
tId = setInterval(function() {
document.getElementById("number").innerHTML = number += increment;
}, speed);
}
function changeSpeed() {
speed = +this.getAttribute("data-speed"); // get attribute and convert to number
inc();
}
window.addEventListener("load", function() { // when page has loaded
document.querySelectorAll(".start").forEach(function(but) {
but.addEventListener("click", function() {
increment = +this.getAttribute("data-inc") ; // get from button and convert to number
inc()
});
});
document.getElementById("stop").addEventListener("click", function() { // when clicking
clearInterval(tId);
});
document.querySelectorAll(".speed").forEach(function(but) {
but.addEventListener("click", changeSpeed);
});
});
<button type="button" class="start" data-inc="1">By 1</button>
<button type="button" class="start" data-inc="10">By 10</button>
<button type="button" class="speed" data-speed="1000">One a sec</button>
<button type="button" class="speed" data-speed="500">Two a sec</button>
<button type="button" id="stop">Stop</button>
<div>
number: <span id="number">0</span>
</div>
function increase() {
setInterval(() => document.getElementById("number").innerHTML = number += 1, 1000)
}
setInterval() will call an anonymous function which increment the number each second (1000 ms) until you call clearInterval()
var number = 0;
function increase () {
number++;
document.getElementById('number').innerHTML = number;
}
Try this out.
I believe the issue is, you're not actually incrementing the variable 'number'. What you're instead doing is setting the content of the element number to number +=1;
So since number is 0;
it will only ever show 1 on the HTML.
If you want the number to increment by one automatically after you've clicked the button once. Create a setInterval.
setInterval(() => {
increase()
}, 5000) // increase every 5 seconds
You can set an interval with setInterval();
var number = 0;
function increase() {
for (var i = 0; i < 1; i++) {
document.getElementById("number").innerHTML = number += 1
}
}
setInterval(increase, 1000);
This should increase the number every second.
If you want to stop it then you can say:
var interval = setInterval(increase, 1000);
clearInterval(interval);
var number = 0;
function increase() {
for (var i = 0; i < 1; i++) {
document.getElementById("number").innerHTML = number += 1
}
}
<button onclick="increase()">
by 1
</button>
<div>
number:
<div id="number">
0
</div>
</div>
var number = 0,
increment = 1,
speed=1000, // one second
tId;
function inc() {
clearInterval(tId); // stop anything already running
tId = setInterval(function() {
document.getElementById("number").innerHTML = number += increment;
}, speed);
}
function changeSpeed() {
speed = +this.getAttribute("data-speed"); // get attribute and convert to number
inc();
}
window.addEventListener("load", function() { // when page has loaded
document.querySelectorAll(".start").forEach(function(but) {
but.addEventListener("click", function() {
increment = +this.getAttribute("data-inc") ; // get from button and convert to number
inc()
});
});
document.getElementById("stop").addEventListener("click", function() { // when clicking
clearInterval(tId);
});
document.querySelectorAll(".speed").forEach(function(but) {
but.addEventListener("click", changeSpeed);
});
});
<button type="button" class="start" data-inc="1">By 1</button>
<button type="button" class="start" data-inc="10">By 10</button>
<button type="button" class="speed" data-speed="1000">One a sec</button>
<button type="button" class="speed" data-speed="500">Two a sec</button>
<button type="button" id="stop">Stop</button>
<div>
number: <span id="number">0</span>
</div>
You can use setInterval(function(){ /* do something every 3 seconds */ }, 3000);
You can call the function in the interval parameter or write the code inside.
You can look at these examples.

How to stop loop after specific number?

I need the auto click to stop after 320 times.
How to do it?
var = "320";
// i need the loop to stop after 320 times.
var button = document.getElementById("jsonp2");
setInterval(function() {
button.click();
}, 10000);
<input type="button" id="jsonp2" href="javascript:void(0)" onclick="javascript:alert('button autoclicked');" class="btn refreshListButton" title="Refresh">
You need to use the clearInterval function
http://jsfiddle.net/pdmafjpa/71/
var iterations = 5;
var count = 0;
var button = document.getElementById("jsonp2");
var myInterval = setInterval(function(){
if (count >= iterations) {
clearInterval(myInterval);
} else {
count++;
button.click();
}
}, 2000);
var i=1;
function a(){
if(i<320)
{
console.log(i);
setTimeout(a,1000);
i++;
}}
a();

How to stop and resume an interval instead of clearing and recall it?

I got this interval function which adds + 1 every second to this element, and a button which stops it with clearInterval(), but i want to "stop" and "resume" the interval instead of "clear" and restarting it. For example if the interval was stopped with 700 miliseconds, so i want to resume it for runnig from these 700 miliseconds and not from 0.
How can i do that?.
var testingB = $('#testing');
var testingBox = $('.text3');
var counter = 0;
var checker = null;
setInterval( function () { testingBox.text(counter); },1);
var id = setInterval( function () { counter++; },1000);
function adder() {
if (checker === null ) {
clearInterval (id);
checker = true;
testingB.text('Keep Counting');
}
else {
id = setInterval( function () { counter++; },1000);
checker = null;
testingB.text('Stop Couting');
};
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" id="testing" onclick="adder()"> Stop Counting </button>
<p class="text3"> </p>

Show Div on Mouseover after three second

I want on first click it will show after 3 seconds but after the first click when I clicked it will shown before 3 seconds:
function Func1()
{
document.getElementById('div1').style.visibility = "visible" ;
}
function Func1Delay()
{
setTimeout("Func1()", 3000);
}
function Func2Delay()
{
document.getElementById('div1').style.visibility = "hidden" ;
}
Your English or description is very poor, but from what I understand, you want something like this :
var div = document.getElementById('div1'),t;
function changeStyle(element,prop,value){
element.style[prop] = value;
}
function showDiv(){
changeStyle(div,'opacity','1');
}
function hideDiv(){
changeStyle(div,'opacity','0');
}
div.addEventListener('mouseover',function(){
t = setTimeout(showDiv,3000);
},false);
div.addEventListener('mouseout',function(){
clearTimeout(t);
hideDiv();
},false);​
Here's a demo : http://jsfiddle.net/gion_13/TSVA5/
You have to hover the "invisible" div and it will show after 3 seconds.
this works for me
the markup:
<input type="button" value="clickme" onmouseout="ClearIntv()" onmouseover="DelayShow('showMe',5000)" />
<div id="showMe" style="display:none;background-color:red;width:50px;height:50px;">
</div>
the script:
var intv;
function DelayShow(timeOutMs) {
var elm = document.getElementById("showMe");
var now = new Date().getTime();
var end = now + timeOutMs;;
intv = setInterval(function () {
now = new Date().getTime();
if (now >= end) {
elm.style.display = "block";
clearInterval(intv);
}
}, 500);
}
function ClearIntv() {
clearInterval(intv);
}
Activates the counter on MouseOver, and cancels on MouseOut

Categories