Userscript Interval [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
var x = document.getElementsByClassName("farm_icon_c");
for(var i=0;i<x.length;i++){
x[i].click();
setInterval(function () {}, 1000);
};
var w=frames.main||self, d=w.document, b=d.links[0].href.match(/(village=)(\d+)/);
w.location.href=w.location.href.replace(/&*village=[pnj]?\d+&*/g, '&').replace(/&+$/,"")+'&'+b[1]+'n'+b[2];
void(0);
see this script works but waaaaay too fast.. This interval function isnt working

I think you want something like
var x = document.getElementsByClassName("farm_icon_c"),
(function f(i) {
if(i < x.length) {
x[i].click();
setTimeout(function() { f(i+1); }, 1e3); // Run next "iteration" in 1 second
} else {
// This code will run at "the end of the loop"
}
})(0); // Run first "iteration" immediately

Related

I can't change data of var element with JavaScript in Html [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Please help me for change data of var element with JavaScript. I can't. I need If check.innerText changed to need change phone number.
function call(){
var check = document.getElementById("locinfo").innerText;
var tellnum = "tel:+998951441000";
if (check=="Катартал") {
tellnum = "tel:+998998047911";
}
else if (check=="Куйлик") {
tellnum = "tel:+998951440010";
} }
window.location.href = tellnum;
}
It's seem like you have one spare },
this should work.
function call(){
var check = document.getElementById("locinfo").innerText;
var tellnum = "tel:+998951441000";
if (check=="Катартал") {
tellnum = "tel:+998998047911";
}
else if (check=="Куйлик") {
tellnum = "tel:+998951440010";
}
window.location.href = tellnum;
}

cannot access the data of the first API in second API function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
<script>
/* 1st API*/
$.getJSON("http://cricapi.com/api/matches/?apikey=6aP8B4MImxSNxI6fevBmC2ZshO42",function(data){
var len = data.matches.length;
for(i=0;i<len;i++){
id = data.matches[i].unique_id;
ms = data.matches[i].matchStarted;
x = data.matches[i].date;
team_1 = data.matches[i]['team-1']
/* 2nd API*/
$.getJSON("https://cricapi.com/api/cricketScore/?apikey=6aP8B4MImxSNxI6fevBmC2ZshO42&unique_id="+id,function(data1){
console.log(team_1);
});
}
});
why i cannot get the team_1 of each match in second getJSON(). It is giving the output of same team name multiple times please help me to get the team names of each match. Appreciate any help!!
This "bug" is caused by hoisting.
Index i is stored as var, and it isn't in local for loop scope, so, when the index has gained its final value, the same value is used.
You can use let keyword:
for(let i=0;i<len;i++){ ..
so the variable will be loop scoped
Also Check out this link
function getScore(id, callback) {
$.getJSON("https://cricapi.com/api/cricketScore/?apikey=6aP8B4MImxSNxI6fevBmC2ZshO42&unique_id="+id, function(data1) {
callback(data1);
});
}
$( document ).ready(function() {
$.getJSON("http://cricapi.com/api/matches/?apikey=6aP8B4MImxSNxI6fevBmC2ZshO42",function(data){
var len = data.matches.length;
for(i=0;i<len;i++){
id = data.matches[i].unique_id;
ms = data.matches[i].matchStarted;
x = data.matches[i].date;
team_1 = data.matches[i]['team-1']
/* 2nd API*/
getScore(id, function(data1) {
console.log(data1);
});
}
});
});

Get line from file - NodeJS function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How to write a function that returns a line from file with nodeJS? The program runs in the loop, and each time you call the function should return a new string. And if the file ends begins again, with the first line.
This function takes randomly, how to do consistently?
var fs = require('fs');
// Get random line from file
function getRandSocks() {
var socks = fs.readFileSync('socks.txt').toString().split("\n");
var randsock = socks[Math.floor(Math.random()*socks.length)];
return randsock.split(':');
}
function loop() {
// ...
var socks = getRandSocks();
console.log('host: '+socks[0]+'port :'+socks[1]);
//...
loop();
}
Perhaps this would help. This gets the next line and wraps back to the start. It is based on the given getRandSocks function with minimal change.
var current_line = 0;
function getNextLines() {
var socks = fs.readFileSync('socks.txt').toString().split("\n");
var randsock = socks[current_line % socks.length];
current_line = (current_line + 1) % socks.length
return randsock.split(':');
}

Randomly selecting a function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am creating just a little test game type thing, and for part of it, I need a function to be randomly selected.
I have 2 altogether, and I just need to create maybe a 'random generator' somehow, which I can call, and it runs one of the 2 functions.
function function1() {
........code.........
}
function function2() {
........code.........
}
function generator() {
...random function selector...
}
Maybe something like that ^
Thanks in advance!
Yes, you can do that.
var myFuncArr = ["function1","function2"];
function generator(){
window[myFuncArr[Math.random() * (myFuncArr.length - 0) + 0]]();
}
JavaScript has an eval function that evaluates a string and executes it as code:
function generator(){
min = 1;
max = 5;
random = Math.floor(Math.random() * (max - min + 1)) + min;
param = "myParam";
eval("function"+random+"('"+param+"')");
}
Do you expecting like this
function method1(){
document.getElementById("sampleDiv").style.color = "red";
}
function method2(){
document.getElementById("sampleDiv").style.color = "yellow";
}
function method3(){
document.getElementById("sampleDiv").style.color = "orange";
}
function generator(id) {
eval("method"+id+"()")
}
function randomIntFromInterval(min,max)
{
return Math.floor(Math.random()*(max-min+1)+min);
}
setInterval(function(){generator(randomIntFromInterval(1,3))},3000);
Demo:
http://jsfiddle.net/hcmY9/3/

How do I use repeated value in innerhtml? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I used this code but it's not repeat
it increase the value but don't get to the initial value 0
please help
i=0;
function loading()
{
text=["Loading.","Loading..","Loading...","Loading...."];
window.setInterval(wr(),500)
}
function wr()
{
if(i<4)
{
wr='document.getElementById("text").innerHTML=text[i]';
alert(i);
}
else
i=0;
return wr;
}
Your code is way to complicated. Define a simple function which you pass to setInterval and make sure you increase the counter variable:
var i = 0; // initialize counter
var textElement = document.getElementById("text");
setInterval(function() {
i = i % 4; // make sure `i` is at max 3 and reset to 0
var text = 'Loading';
for (var j = i; j--; ) {
text += '.'; // add the correct number of periods
}
textElement.innerHTML = text; // set text
i++; // increase counter
}, 500);
DEMO
Here's a slight modification of a similar answer I gave a while back.
var i = 0;
setInterval(function() {
i = ++i % 4;
document.getElementById('text').innerHTML = "Loading"+Array(i+1).join(".");
}, 500);

Categories