arguments.callee question - javascript

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

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.

JavaScript Async Call only works with alert()

My code only works properly with inside
alert(tickerSymbol);
Im new to JavaScript so Im not sure why this is happening. Can Someone help me understand why the timeout of the alert fixes my problem?
database.ref("users")...
Without the alert my second firebase call above is never implemented.
function quote_search(json) {
var database = firebase.database();
var tickerSymbol = json.symbol;
var userId = firebase.auth().currentUser.uid;
alert(tickerSymbol);
database.ref('users/' + userId).update({
"current_ticker": tickerSymbol
});//end firebasecall
database.ref("users").orderByChild(userId + "/current_ticker").on("child_added", function(snapshot) {
var userCurrentTicker = snapshot.val().current_ticker;
var test = localStorage.setItem('tickerStored', userCurrentTicker);
});//END FIREBASE
location.reload();
}
An alert blocks the code from running until it is dismissed. Because of this, the later code hasn't yet run while the alert is being displayed.
I don't know firebase, but I'd wager that something before your alert takes some time to complete, and that you shouldn't run the later code until it does. You might find the same issue between the two queries.
You should look at the documentation for the various methods you've used, and see if they return promises, or accept callbacks as arguments. In either case, these are mechanisms to wait for something to be finished before doing something else, and you will likely find the bug disappears if you make use of them.
If that's not the issue, it could be that your location.reload() is causing the browser to reload the page before the response has come in from the server and so the child_added event has fired. Try moving or removing the location.reload() call.

Google chrome webrequest conflict?

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);

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.

Implementing "this is taking too long" message with jQuery

How to implement a gmail-like "this is taking too long" warning message using jQuery Ajax API?
For those who have never seen this message on gmail, it appears when the 'signing in' process takes too long for completion, and then some solutions are suggested.
I'm using jQuery Ajax on my website and I want to warn users when page loading is very slow and then suggest some solutions (a link to refresh the page, or to a help page, for example).
I'd suggest something as simple as this arrangement:
function tooLong() {
// What should we do when something's taking too long? Perhaps show a `<div>` with some instructions?
$("#this-is-taking-too-long").show();
}
// Then, when you're about to perform an action:
function performSomeAction() {
var timer = setTimeout(tooLong, 10000);
$.get('/foo/bar.php', {}, function() {
// Success!
clearTimeout(timer);
});
}
Why not just use the built-in jQuery ajax 'timeout' option. It's a good practice to use it anyways, in case you have issues with your ajax call. Or, you could re-invent the wheel ;)
edit: and, er, I think you would want to couple that with an error function.

Categories

Resources