I want to create a function such that
each time the function runs, the value of variable P will increase;
the number of times the function runs is set by the variable runtimes.
For example:
var runtimes = '25';
var P = '1';
function send(){
//some function
}
After the first run, variable P will be 2 and the second run will start.
How can I do this?
You can call the function from a loop:
var runtime = 25;
var P = 1;
function send(){
P++;
}
for (var i = 0; i < runtime; i++) {
send();
}
Or keep track of the iterations in the function itself and call it once:
var iterations = 0;
var runtime = 25;
var P = 1;
function send(){
P++;
if (++iterations < runtime) {
send();
}
}
send();
var runtime = 25;
var P = 1;
for(var P = 1; P < runtime; send(), P++) { }
function send(){
//some function
}
function sendMultipleTime(){
var runtime = 25;
var P = 1;
while(P<=25)
send();
P++;
}
function send(){
}
You can use a simple for loop, if I've understood the question correctly.
var runtime = 25;
var P = 1;
for (var i = P; i <= runtime; i++) {
function send() {
console.log(i + ' ' + P + ' ' + runtime);
}
send();
P++;
}
Related
I have the following function.the code does what i need it to do,however its not structured correctly because of the declarations therefore it gives "(ESLint) move function to body root" error :
This is my code:
function onSuccess(result) {
if (result) {
function GetTotal(result) {
var totBal = 0;
var str = 'R';
for (var i = 0; i < result.length; i++) {
totBal += result[i].Price;
}
str += totBal.toFixed(2);
return str;
}
var span = document.getElementById("Total");
span.innerHTML = GetTotal(result);
}
}
This is what i tried:
function onSuccess(result) {
var fn;
if (result) {
fn=function GetTotal(result) {
var totBal = 0;
var str = 'R';
for (var i = 0; i < result.length; i++) {
totBal += result[i].Price;
}
str += totBal.toFixed(2);
return str;
};
var span = document.getElementById("Total");
span.innerHTML = GetTotal(result);
}
}
the "(ESLint) move function to body root" gets solved But with the above it returns an error saying "getTotal is not defined"
Then function declaration is on right hand side. It becomes a local variable to the variable on left hand side. Use the name GetTotal instead of fn in the main function scope.
function onSuccess(result) {
var GetTotal;
if (result) {
GetTotal = function(result) {
var totBal = 0;
var str = 'R';
for (var i = 0; i < result.length; i++) {
totBal += result[i].Price;
}
str += totBal.toFixed(2);
return str;
};
var span = document.getElementById("Total");
span.innerHTML = GetTotal(result);
}
}
You can just move the function to the body root as suggested by ESLint like so
function onSuccess(result) {
function GetTotal(result) {
var totBal = 0;
var str = 'R';
for (var i = 0; i < result.length; i++) {
totBal += result[i].Price;
}
str += totBal.toFixed(2);
return str;
}
if (result) {
var span = document.getElementById("Total");
span.innerHTML = GetTotal(result);
}
}
Or just get rid of the function as it's inside of a function anyway and you don't seem to be using it again.
function onSuccess(result) {
if (result) {
var totBal = 0;
var str = 'R';
for (var i = 0; i < result.length; i++) {
totBal += result[i].Price;
}
str += totBal.toFixed(2);
var span = document.getElementById("Total");
span.innerHTML = str
}
}
On your second example the function is now inside the variable fn. You can access it by calling fn(). Another option is to rename the variable to reflect the function behavior and then call it by its new name.
var fn;
fn=function GetTotal(result) {
console.log('function called!');
}
fn();
Simgply add rules to disable the eslint rule. I.E. add "no-inner-declarations":[0, "always"], in .eslintrc.json
I am trying to make an interactive stack application using javascript in browser but I cannot clear my console after every actions.
<script>
var st1 = 10;
var stk1 = new Array(10);
var count = 0;
function push1(v) {
if (st1 === 0) {
rpiseconsole.log("Stack Overflow");
}
else {
st1 = st1 - 1;
stk1[st1] = v;
print1();
}
}
function pop1() {
var temp = stk1[st1];
st1 = st1 + 1;
print1();
return temp;
}
function print1() {
for (var i = st1; i < 10; i++) {
rpiseconsole.log(stk1[i]);
}
};
function doJob() {
var x = document.getElementById("t").value;
push1(x);
document.getElementById("t").value = "";
}
function doJob1() {
var p = pop1();
document.getElementById("t1").value = p;
}
</script>
I am expecting that every time I click on the push button it will print the array of stack and clear the previous results in the console and when I click on the pop button it will take out the last data and print the remaining array into the textarea.
I cannot get the clear option in console and I cannot output my array to the textarea in the same order as I put items into the stack.
Write a method clearConsole() and invoke it before every print command
function clearConsole() {
document.getElementById( "rpiseconsole" ).innerHTML = "";
}
Demo
var st1 = 10;
var stk1 = new Array(10);
var count = 0;
function push1(v) {
if (st1 === 0) {
rpiseconsole.log("Stack Overflow");
} else {
st1 = st1 - 1;
stk1[st1] = v;
print1();
}
}
function pop1() {
var temp = stk1[st1];
st1 = st1 + 1;
print1();
return temp;
}
function print1() {
clearConsole();
for (var i = st1; i < 10; i++) {
rpiseconsole.log(stk1[i]);
}
};
function doJob() {
var x = document.getElementById("t").value;
push1(x);
document.getElementById("t").value = "";
}
function doJob1() {
var p = pop1();
document.getElementById("t1").value = p;
}
function clearConsole() {
document.getElementById( "rpiseconsole" ).innerHTML = "";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js">
</script>
<div>Procedural Stack with Tight Coupling</div>
<div id="rpisesetup">
<textarea id="rpiseconsole"></textarea>
<script src="https://sites.google.com/a/rajeshpatkar.com/library/hub/rpiseconsole.js">
</script>
<textarea id="t"></textarea>
<textarea id="t1"></textarea>
<button onclick="doJob()">push</button>
<button onclick="doJob1()">pop</button>
</div>
when trying to get items from local storage that are named
useJSON1, useJSON2 and so on.
i get an infinite loop.
var test = 0;
function loadTasks() {
let i = 1
let taskObject = JSON.parse(localStorage.getItem('useJSON' + i));
while (test < i)
if (taskObject) {
// do somthing;
i++;
} else {
test = i;
}
}
Have you checked your syntax, and brackets?
Should it look more like this?
var test = 0;
function loadTasks() {
var i = 1;
var taskObject = JSON.parse(localStorage.getItem('useJSON' + i));
while (test < i){
if (taskObject) {
`do somthing`;
i++;
} else {
test = i;
}
}
}
This issue is asked already some times. But in the application of google apps script i can't solve this problem.
function two ()
{
var bridgeclubs = SpreadsheetApp.openById("1dfyI1jbz..........TVg4OoixKTz1");
var bridgeclubs_sheet = bridgeclubs.getSheetByName("Sheet1");
var data_bridgeclubs = bridgeclubs_sheet.getDataRange().getValues();
var numRows = bridgeclubs_sheet.getDataRange().getNumRows();
var sprshtname = SpreadsheetApp.getActiveSpreadsheet().getName();
for (var i=1; i<=numRows; i++)
{
if (sprshtname == data_bridgeclubs[i][6])
{
var A = i;
break;
}
}
}
In function one I do a call to function two, where I need this value A:
function one ()
{
two ();
//here I need this value A;
var C= 35 * A;for instance
}
Who can help me?
You can return value from function using return i; and obtain it in within one by var A = two();.
function two() {
var bridgeclubs = SpreadsheetApp.openById("1dfyI1jbz..........TVg4OoixKTz1");
var bridgeclubs_sheet = bridgeclubs.getSheetByName("Sheet1");
var data_bridgeclubs = bridgeclubs_sheet.getDataRange().getValues();
var numRows = bridgeclubs_sheet.getDataRange().getNumRows();
var sprshtname = SpreadsheetApp.getActiveSpreadsheet().getName();
for (var i=1; i<=numRows; i++) {
if (sprshtname == data_bridgeclubs[i][6]) {
return i;
}
}
}
function one() {
var C;
var A = two();
if (A) {
C = 35 * A;
}
}
You can use global variables.
var A;
function two() {
var bridgeclubs = SpreadsheetApp.openById("1dfyI1jbz..........TVg4OoixKTz1");
var bridgeclubs_sheet = bridgeclubs.getSheetByName("Sheet1");
var data_bridgeclubs = bridgeclubs_sheet.getDataRange().getValues();
var numRows = bridgeclubs_sheet.getDataRange().getNumRows();
var sprshtname = SpreadsheetApp.getActiveSpreadsheet().getName();
for (var i=1; i<=numRows; i++) {
if (sprshtname == data_bridgeclubs[i][6]) {
A = i;
break;
}
}
}
function one() {
two();
var C= 35 * A;
}
Consider the following code:
function func() {
var totalWidths = 0;
for( var i = 0, count = arr.length; i < count; i++ ) {
var image = arr[i];
insertElemInDOM(image);
preloadImage(image,function(){
var w = image.width();
totalWidths += w;
});
}
// do something with the variable "totalWidths"
doSomething(totalWidths)
}
I have 2 problems here. The image will be always the same (first problem), which one can solve with an anonymous function:
for(...) {
(function(image) {
preload(image,function() {
// now image is the correct one
});
})(image);
}
But how do I manage the totalWidths variable in order to use it later on doSomething(totalWidths)? The previous code would have a value of 0 for totalWidths.
Thanks!
You could timeout the whole loop and the doSomething, that's much more performant than setting up so many timeouts:
setTimeout(function() {
var inc = 0;
for (var i = 0; i < count; i++) {
var w = arr[i].width();
inc++;
}
doSomething(inc);
}, 1000);
However, what you actually seem to want are nested timeouts, i.e. waiting 1s for each iteration step and doing something after all have finished:
var inc = 0, count;
function asyncLoop(i, callback) {
if (i < count) {
var w = arr[i].width();
inc++;
setTimeout(function() {
asyncLoop(i+1, callback);
}, 1000);
} else {
callback();
}
}
asyncLoop(0, function() {
doSomething(inc);
});
OK, now that we know what you need the solution is to check after each load event whether all images are loaded:
var totalWidths = 0,
count = arr.length,
loaded = 0;
for (var i = 0; i < count; i++)
(function (image) {
insertElemInDOM(image);
preload(image, function() {
totalWidths += image.width();
// counter:
loaded++;
if (loaded == count-1) // the expected value
doSomething(totalWidths); // call back
});
})(arr[i]);