Google chrome webrequest conflict? - javascript

Can someone please explain to me why and if it is the desired behavior that the following code WILL NOT ALWAYS alert the same value twice in a row when visiting webpages assuming a small delay in clicking "ok" on the alert box?
Usual simple extension set up with the following code in background.js:
chrome.webRequest.onBeforeRequest.addListener(function(request) {
alert(request.requestId);
alert(request.requestId);
return {};
}, {urls: ["*://*/*"]}, ["blocking"]);
I just cannot get this. I need the same value to pop up twice because I will be doing AJAX requests and I cannot permit myself to have variables changing behind my back with potential delays in AJAX...
If it is the desired behaviour how can I make sure this does not happen? Would closures help?
Thanks

The most likely reason is that the pop-ups are coming from two different requests that are happening at the same time. There can be multiple requests for each underlying HTTP request, for example. Instead of using pop-ups, you can log via the console.log() function.

var rId = request.requestId;
alert(rId);
alert(rId);

what about this???
var rId = request.requestId.toString();
alert(rId);
alert(rId);

Related

How to handle multiple requests being sent in JavaScript?

Working on a platform, to enable auto-ticketing functionality. For which a REST API request is used for ticket creation. Unfortunately, there are 2 requests popping simultaneously, which results in creating duplicated tickets.
How to handle such case and send only one of these requests?
Tried adding the 2nd request in the response callback of the first, though this does not seem to work.
if (flag == 1){
logger.debug("Node-down alarm-Request raised - +sitn_id);
clearTimeout(mouseoverTimer);
mouseoverTimer = setTimeout(function(){
logger.debug("Inside Call back function - ");
//function call for ticket creation
incidentRequest(sitn_id,confUtil.config.mule_url);
}, 10);
You really should show more of the code that makes the request, though it seems as if you are doing some ajax inside your 'incidentRequest', so I will presume that (if that isn't what you are doing, then please, show your code....) - and since you tags say javascript and jquery - well, here goes...
To stop the 'double send' in an AJAX call, it is simple:
function incidentRequest(sitn_id,confUtil.config.mule_url){
// stop the double by clearing the cache
$.ajaxSetup({cache: false});
// continue on with the AJAX call
// presuming the url you want is confUtil.config.mule_url
// and the data you want to send is sitn_id
$.post(confUtil.config.mule_url, 'sitn_id=' + sitn_id, function (data) {
// do cool stuff
});
}
Hopefully that will help you get moving. If not, then we will need more code of what is going on around all this.

sending ajax request but not getting right result.Js

i am trying to display the data fetched from database in the loop and between loop i call the function and send ajax request its not working.Actually its displays the only if i used alert command. If i used alert then the browser display the div and then alert if i clicked ok then it displays the second div then again show alert.
Here is the js code
function like(divid,id,session) {
var orgnldiv=document.getElementById(divid);
var ndiv=document.createElement('DIV');
var idd=id+5000;
ndiv.id =idd;
ndiv.className="likeclass";
orgnldiv.appendChild(ndiv);
var dynamicdiv=document.getElementById(idd);
var span=document.createElement('span');
var spanid=idd+5000;
span.id=spanid;
span.className="spanclass";
dynamicdiv.appendChild(span);
var xmllhttp15;
if (window.XMLHttpRequest) {
xmlhttp15=new XMLHttpRequest();
} else {
xmlhttp15=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp15.onreadystatechange = function() {
if (xmlhttp15.readyState==4 && xmlhttp15.status==200) {
document.getElementById(spanid).innerHTML=xmlhttp15.responseText;
}
}
xmlhttp15.open("GET","spancount.php?postid="+id+"&userid="+session);
xmlhttp15.send();
// alert(spanid);
}
please suggest me what can be the reason of this problem my code is working well only if i use alert
The reason why your code works when you use alert is because whenever the alert function is called. The program flow is paused. In other words, your loop wont continue to make another Ajax call until you dismiss the alert.As a result, the request gets handled properly and the response data appears in the span div. that is why I had mentioned to make your calls synchronous instead.
So to answer the question you asked in the comment, Yes at times too many Ajax calls can be a problem. Let's say that the loops runs more than 15-20 times, that means 15-20 simultaneous requests. Now, think about the number of times the same request is being handled by the php script? Definitely a problem here!
Even with Jquery Ajax, the chances of the loop completing successfully is also 50-50 actually because it all boils down to the amount of requests being made , the bandwidth being used and how the request is being processed at the server.
One possible way to fix this problem is : Rather than constantly requesting small peices of data again and again from the server in the loop, Make one Ajax call and get the entire data as json. Then, parse the json and append data to the spans by using the particular span id to extract the relevant data from the json object.
You might have to do a little bit of tweaking in both the above javascript and spancount.php . But it will definitely Save you A LOT of bandwidth. You gotta consider the fact that more than one person could be using your site!!
Hope that cleared up things, all the best with your project :D

jQuery Ajax, prevent the refresh button when the ajax is in progress

Hi i have to perform perform like, when the ajax is in progress, then do not allow the user to do page refresh.
here is the code i have
$('#submit').click(function() {
$(function() {
$(".col1").mask("Generating csv...."); //This will generate a mark, Here i would like to prevent the user from doing any sort of operation.
var to = $('#filters_date_to').val();
var from = $('#filters_date_from').val();
$.ajax({
url:"../dailyTrade/createCsv?filters[date][to]="+to+"&filters[date][from]="+from,success:function(result){
if(result) {
$(".col1").unmask(); //Here we can unlock the user from using the refresh button.
window.location = '../dailyTrade/forceDownload?file='+result;
setTimeout('location.reload(true);',5000);
}
}
});
});
});
Any suggestions.
Best you can do is use onbeforeunload to present the user with a message saying that a request is in progress and asking them if they are sure they want to proceed.
e.g.
var req;
window.onbeforeunload = function() {
if(req) {
return 'Request in progress....are you sure you want to continue?';
}
};
//at some point in your code
req = //your request...
You cannot, in any way, prevent the user from leaving your page using JS or anything else.
I doubt if you should do that.
$(window).bind('beforeunload',function(){
return 'are you sure you want to leave?';
});
If you are talking about a refresh "html button" on your web page, that can easily be done. Just before you make your ajax call, disable your refresh button and on success/error function of the ajax call enable it.
Disable button
$("#refreshBtn").attr("disabled", "disabled");
Enable button
$("#refreshBtn").removeAttr("disabled");
You cannot do it just by inserting JavaScript code.
Only ways I can think of are:
Use synchronous ajax call, on that way browser should freeze (however it will notify user that script is taking too long to process and user will be able to stop execution)
Write browser plugin that will modify browser behavior (eg. prevent refreshing page for url that you preset)
Both ways are ugly and I wouldn't recommend doing it.
You should modify your script so it can resume execution if page has been refreshed (use HTML5 localStorage).
http://www.w3schools.com/html/html5_webstorage.asp
In your case, I would put in localStorage simple state (boolean) to check did ajax call happened or not. If it did happened, then just try calling again same url and you will get file name. But on server side (if you haven't done already) you should implement caching, so when same url is called twice, you don't need to make two separate files, it could be same file (server will be much lighter on hardware resources).

jQuery Ajax and setInterval Chrome Issues

I'm trying to a return the value of a given URL periodically using jQuery and setInterval. My code looks like:
$("form").submit(function() {
setInterval(function(){
$('#upload_progress').load('/upload_progress');
}, 5000);
});
This works perfectly in Firefox, but in chrome, the load() function never runs. I've treid using the $.ajax function as well with the same result.
Any ideas why this is only affecting Chrome (v11.0)?
Any help would be much appreciated!
For one, you are actually submitting the form. I'm pretty sure this places the browser in a state of "hey, i'm waiting on a redirect from the server." If you really want to poll and update the page, you probably need to do
$("form").submit(function(e) {
e.preventDefault();
// ...
}
Just as a start. In this context, it works for me. Here, I even made you a pretty little JSFiddle of it working: http://jsfiddle.net/plukevdh/sRe4k/. If you need redirection once complete, you might add more data to the callback (json or something) so that you can check to see if {status: 0-100 [percent], completed: true|false} and if completed or status >= 100, just change the window.location.
Is your code wrapped in a document ready check?
$(document).ready(function(){
// your code
});
If not, that may be why.

arguments.callee question

I know it's possible to call the calling function, but is it possible to call the function calling that function. Ok ... that sounds a little confusing. Let me demonstrate:
pop.share(msg, function(response) {
if(response) response = true;
else response = false;
});
Basically a box pops up to ask the user to share. If the response is false I want to call pop.share ... thus displaying the popup modal forcing them to share. Ok, this is probably not good logic or practice for a live site.
I was just lying in bed and I got a though "can that actually be done". I was trying and trying with some test code and couldn't figure it out.
Edit: A do while would not work if it was a modal as it's not waiting for the users response, thus creating an infinite loop.
Try obsolete arguments.caller? But since it is obsolete, it is not useful for live site.
https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Functions_and_function_scope/arguments/caller
Try not obsolete arguments.callee.caller

Categories

Resources