I put a code will reload the current page in chrome console.
eg..
(function myLoop (i) {
setTimeout(function () {
console.log(i);
if (--i) myLoop(i);
}, 3000)
})(10);
After running above code, the loop runs only one time and the console reloaded.
how can i do this?
I put a code will reload the current page in chrome console.
eg..
(function myLoop (i) {
setTimeout(function () {
console.log(i);
if (--i) myLoop(i);
}, 3000)
})(10);
After running above code, the loop runs only one time and the console reloaded.
how can i do this?
Share Improve this question asked Jul 17, 2016 at 10:51 yolohoamyolohoam 312 silver badges7 bronze badges 2- Check this answer: stackoverflow./questions/26380086/… – KooiInc Commented Jul 17, 2016 at 11:35
- 1 what exactly are you trying to achieve? seems like localStorage should do the trick – mylee Commented Jul 17, 2016 at 11:36
3 Answers
Reset to default 4The function you provided counts down from 10 to 1, with a 3 second interval. If you are running that directly in the Chrome Console and you refresh your page in the middle, it will of course be killed because the function is executing in the context of the current page you have loaded.
If you want to persist the loop between reloads when the code is running within the page (not the console), then you could maintain the state of the variable when you are executing the code, e.g. in local storage or a cookie. When the page loads, use the stored value instead of the default.
Example:
window.onload = function() {
var count = localStorage.getItem('count');
if (count == null) count = 10;
(function myLoop(i) {
setTimeout(function() {
localStorage.setItem('count', (i-1));
console.log(i);
if (--i) {
myLoop(i);
} else {
localStorage.removeItem('count');
}
}, 3000)
})(count);
}
You may create a chrome extension, it can keep executing after reload
As first, why do you want to do this? It would be very weird you want to reload a webpage, why? If it is used for internet speed purposes i would prefer:
cmd.exe /C ping (target) /t 00 /n 65000
But you can try to use a loop statement for it. I'm not so familiar with C#, so i searched it up on internet.
int number = 0;
while(number < 5)
{
// yourcode
number = number + 1;
}