Can you please explain me the following javascript? - javascript

I've been using this code to buy a phone in a flash sale and now that I'm done with with it I want to understand what the following code actually does:
setInterval(function (){
jQuery('.btn').trigger('click');
console.log('Working...');
}, 10);

This code is setting interval of the function using setInterval meaning that the function will be called after every 10 millisecond. The function in it is triggering the click of the element having class btn and then sending data to the log ("WORKING");
setInterval(function(){ jQuery('.btn').trigger('click'); console.log('Working...'); },10);
Hope this helps you

It enforce click on element with .btn class after 10 miliseconds.

Related

setInterval(function, timer) become to infinite loop when click Enter key

I have my set interval function and its working fine, but sometimes after setintervel trigged and I pressed Enter key some 4 to 5 times and my interval become infinite. can any one help me on this.
Thanks in advance.
code:
/one of the js file/
var intervaltime=setInterval(functionname(), 1000);
functionname()
{
if(pageloaded== "true"){ //pageloaded is coming from one JSP when I click that page.jsp
clearInterval(intervaltime);
}
}
**//page.jps**
<input type="hidden" value="true" id="pageloaded" name="pageloaded" />
page.jsp:
init method I added the hidden variable and to set the value of
First of all, the setInterval Method needs a function as first parameter, not "function()" .
For example :
setInterval(function() {
console.log("hello");
}, 1000);
You can also use the following syntaxe to declare your function ouside the timer :
function yourfunc() {
// ...
}
setInterval(yourfunc, 1000);
Also, I think that what you are trying to do is to stop something when your input is clicked or when your page is loaded. If so, the timer is not a good way to do that. You should use an event listener instead.
I hope it helped

Timer in div / event to specific time

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
}
}

click a button programmatically in a website

Well i want to build a bot thats clicks a button after amount of time like every 5 hours,
how can i do that?
i prefer in PHP , but if there any other language that you know , i know also C#,Java,JS..
any suggestions to start, i just never did it before and i want to do learn how to do it very much, i have been searching, didn't find nothing.
Thank a lot!.
I already tried in chrome what the button send , and it send javascripts:void(0) something like and location.href = /Blah/blaahh.php.
So basically your question is programmatically click a button and trigger it in a certain time interval. So what came up to my mind was the solution below:
<script>
setInterval(function(){
$('#button').trigger('click');
}, 3000);
</sctipt>
The code above will trigger and the button will be triggered as clicked and it will take place for every amount of time. After the button is being clicked, try to add some function to handle the event after the button is technically being clicked.
Hope it helps =)
You must use setTimeout on every n < 10 seconds to prevent the browser from sleeping, setInterval with hours not a good idea.
In my opinion, The best two ways to implement something like that is:
Node.js
setTimeout every 10 seconds and loop on the same function, and each time check if the last click was 5 hours ago.
Javascript Example setTimeout:
var CLICK_EVERY_TIME = 10 * 1000; // i choosed to click every 10 sec
var clickCount = 0;
var lastClick = 0;
var loops = 0;
$(window).ready(function(){
// on page load start you auto clicker
runner();
});
function performClick(){
clickCount++;
$('#clickCount').html("clicks: "+clickCount);
// save last click time
lastClick = new Date().getTime();
}
function runner(){
loops++;
var currentTime = new Date().getTime();
if(currentTime - Number(lastClick) > CLICK_EVERY_TIME){
performClick();
}
$('#loops').html("runner: "+loops+" loops");
setTimeout(runner, 1000); // every 1 seconds
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="clickCount">clicks: 0</div>
<div id="loops">runner: 0 loops</div>
Node.js:
You have to use jsdom to simulate a browser, and perform your clicks.
Note: In Node.js you can use setInterval with hours as mush as you want, because the Node.js on standalone process.

Starting, stopping & refreshing timer javascript

I have a little fiddle here where I'm starting/stopping/resetting a javascript timer.
The functionality needs to be a timer runs on a page. When the timer is up, it sends a message, then restarts. The stop button will stop the timer completely.
The fiddle above has the functionality I just described, however I feel like I'm not doing this correctly. Is setTimeout the correct way to create this timer? Should I use setInterval instead?
Secondly, my reset code looks like :
var onReset = function() {
clearTimeout(timerHandle);
onStart();
};
Is there a more elegant way to reset a timer in javascript?
Thanks.
The only improvement I can offer is for you to put it all in an encapsulated object, ask if you want an example. Or if you want to keep the structure you've got then change your onStart function to this to remove a bit of un-needed code.
var onStart = function() {
timerHandle = setInterval(function() {
$("#console").append("timer fired.. <br />");
}, 2000);
};
Fiddle here http://jsfiddle.net/qx6CM/

jQuery addClass removeClass with timer

I have an issue with a 'Latest News' module. Please have a look at http://www.proudfootsupermarkets.com/ to see an example of the module (it's the div close to the top of the page which has a large image in it).
At the moment I have it set up so that when a user clicks on a tab the main article shows. The jQuery for this is:
jQuery(document).ready(function() {
$(".moduletable.latestnews article:first-child").addClass("atfront")
$(".moduletable.latestnews article").click(function(){
$(".moduletable.latestnews article").css("zIndex",1).addClass("atback").removeClass("atfront");
$(this).css("zIndex",100).addClass("atfront").removeClass("atback");
});
});
This is all quite simple and straight forward. The problem that I am having is that I want the articles to change automatically after a few seconds. This would then go in an infinite loop starting with article 1 and then after a couple of seconds showing article 2 etc etc...
I am sure that this is fairly simple but I have just about exhausted my knowledge of JavaScript. Thank you for any help that you are able to give :)
I have created a jsfiddle http://jsfiddle.net/Bempv/
You can use setTimeout to change the article in front. If you select the article with the class ".atfront" and then use the .next() selector you should get the functionality you are looking for
$(function(){
var articleToggler = function articleToggler(){
var front = "atfront",
back = "atback";
return function(){
var selection = $(".moduletable.latestnews")
.find("article.atfront")
.addClass(back)
.removeClass(front);
next = selection.next("article");
next = next.length ? next : $(".moduletable.latestnews")
.find("article").first();
next.addClass(front)
.removeClass(back);
console.log(selection.length,next[0])
setTimeout(articleToggler(),1000); //changes every second
};
};
//start the rotation
(articleToggler())();
});
The setTimeout will call the function passed as the first argument once the timeout expires. The timeout argument is in miliseconds so the above code wait for 1 second before calling the function. Since the above function adds it self as the callback this will repeat indefinately

Categories