I am trying to solve this issue and can’t seem to find any answers on the web or anywhere. The task is simple. I am using console in Chrome and I am trying to execute Javascript code that will execute the code between pages. As a simple example trying to navigate and pause between pages seems like impossible task.
var increment = 1;
var miliseconds = 2500;
setTimeout(function () {window.open('www.google.com', '_self');}, miliseconds * increment);increment++;
// Do something or grab values on this page
setTimeout(function () {window.open('www.yahoo.com', '_self');}, miliseconds * increment);increment++;
// Do something or grab values on this page
setTimeout(function () {window.open('www.cnn.com', '_self');}, miliseconds * increment);increment++;
Tried event listeners and no luck. Any help?
Unfortunately this is not possible. The console's environment will clear every time you navigate. The most you can do is preserve logs, but that will not allow you to continue executing JS defined on the previous page.
The only way I know of to persist values accross pages is to use the window.name variable which will remain accross different pages, but this is pretty hacky.
If you're looking for something a bit more permanent, I would recommend writing a chrome plugin instead.
Related
My own Google Chrome extension stops working after some time, because setTimeout() is not working in some cases.
Therefore the background script is not sending a message to the content script after some necessary delay.
I have found this problem by writing many console.log() statements.
As a solution i thought about using setInterval() until the message is sent to the content script:
var timer = setInterval(function() {
clearInterval(timer);
// Sending a message to the content script
}, 3000);
My code before, during and after setInterval() is quiet long, so i hope this code snippet is somehow enough.
Does anyone know in which cases these Timeouts or Intervals do not start?
Or are there any similar options i could use?
I have thought about using the Chrome Alarms API instead, but alarms can only be used once every minute and the minimum delay is one minute if i am not mistaken.
I never experienced any issue of setinterval or settimeout stops working. I guess there might be an issue in your code. If you are sure about the bug you should make a demo project and report bug on github.
I have a function that will reload the current page after a period of time. I want this function to run automatically every time the page is reloaded (using the debugger).
function reloadPage() {
window.location.reload(false);
}
setInterval(reloadPage,3000)
The problem is that every time the page is reloaded, the code in the debugger will be cleaned and the function will not be called. How to fix this?
A very simple solution: Rather than putting the Javascript into the console, consider putting it in your application but disabling it when you're not debugging.
For example, you could have a GET parameter in your URL that, when present, triggers the function. A good explanation of how to retrieve a GET parameter in Javascript is at How to retrieve GET parameters from javascript?
An even simpler alternative would be to simply leave this code commented out, and comment it in when you want to debug. (This is not a good practice and I will scold you for it during code review, but it is a real thing that real people do, and it has the advantage of being easy and working.)
-
An alternative: You could detect when the console is open, and only run your code when the console is detected (though this would annoy power users like me who tend to always have developer tools open). It's not trivial to detect, but there's a library you can use: https://github.com/zswang/jdetects
I downloaded a website and I am going to work with offline. But I want it to be like as on the server. I mean the load time. While I am working on offline all items load very quickly. I want make it like on the image.
what can i add to html css javascript files. is there a sleep() or wait() function that i can use?
Do not do that, as it will require great effort with questionable result. If you want to measure performance, the only correct way is to do it on server.
As javascript isn't multithreaded, you can't make the website to stop everything and wait but what you can do is, create a function for your own:
function sleep(delayTime) {
var start = new Date().getTime();
while (new Date().getTime() < start + delay){
// do nothing
}
}
Call this function wherever you want with a number representing the delay that you want.
Let's say I have this javascript function:
function pauseComp(ms) {
var date = new Date();
var curDate = null;
do { curDate = new Date(); }
while(curDate-date < ms);
}
and a css3 animation (for instance, <i class="icon-spinner icon-spin"></i> from the new font-awesome 3). When I run the javascript function above, it stops the spinner while the function is running. See what I'm talking about here. Basically, javascript stops css animations, and I'm wondering why, or if anyone else has noticed this/found a workaround. I've tried putting it in a setTimeout(fn,0), where fn is the long process, but then realized why that will also not work (js is not multithreaded). Anyone seen this happening?
Update: Interestingly, it looks like this isn't as much of a problem in Safari, although interaction with the browser interface is still being affected.
A browser page is single threaded. Updating the UI happens on the same thread as your javascript program. Which also means that any animation will not draw new frames while Javascript code is being executed. Typically, this is no big deal because most JS code is executed very quickly, faster than a single animation frame.
So the best advice is simply this: Don't do that. Don't lock up the JS engine for that long. Figure out a cleaner way to do it.
However, if you must, there is a way. You can get an additional thread via HTML5's Web Workers API. This isn't supported in older browsers, but it will allow you to run some long running CPU sucking code away from the main webpage and in it's own thread, and then have it post back some result to your page when it's done.
I am creating a really big JavaScript object on page load. I am getting no error on firefox but on Internet Explorer I am getting an error saying:
Stop running this script ?
A script on this page is causing your web browser to run slowly. If it continues to run, your computer might become unresponsive.
Is there any size limit for Javascript objects in Internet Explorer ? Any other solutions but not dividing the object?
The key to the message your receiving is the "run slowly" part, which relates to time. So, your issue is not object size, but the time taken to construct the object.
To refine things even further, the issue is not the time taken to construct the object either. Rather, IE counts the number of javascript statements it executes, resetting this count when it executes an event handler or a setTimeout function.
So, you can prevent this problem by splitting your code into multiple pieces that run inside calls to setTimeout(...);
Here's an example that may push you in the right direction:
var finish = function(data) {
// Do something with the data after it's been created
};
var data = [];
var index = 0;
var loop;
loop = function() {
if (++index < 1000000) {
data[index] = index;
setTimeout(loop, 0);
} else {
setTimeout(function(){ finish(data); }, 0);
}
}
setTimeout(loop, 0);
The resources available to JavaScript are limited by the resources on the client computer.
It seems that your script is using too much processing time while creating that object, and the 'stop script' mechanism is kicking in to save your browser from hanging.
The reason why this happens on Internet Explorer and not on Firefox is probably because the JavaScript engine in Firefox is more efficient, so it does not exceed the threshold for the 'stop script' to get triggered.
that is not because of the size but because of the big quantity of loops you are executing and the big execution time. if you cut it into smaller parts it should work ok.
Try lowering the complexity of functions your running. Can you post it so that we can look at the loops and try to help?
Edit:
I supose you want to do all that on the client side for some reason. The code seems to need to much execution to be runing on the client side.
Can't you do the calculations on the server side? If this is all to initialize the object, you can cache it to avoid reprocessing and just send a generated json to the javascript side.
It does seem cachable
You must be using big loops or some recursive logic in the code. It really doesn't depend on the size of the object—it depends on the CPU resources it uses (memory, processor, etc.).