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.
Related
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.
I have the code and it is not working on first click, but on the second click it is working.
$("#btnCopiar").on("click",function(){
var clipBoardObj = new ZeroClipboard($("#btnCopiar"), {
moviePath: "../thirdparty/ZeroClipboard.swf"
});;
// Create your data here to copy to the clipboard and assign to a variable name data
var data = "DATA IS COMING FROM SERVER OT TEXT INPUT";
clipBoardObj.on("copy", function (event) {
var clipboard = event.clipboardData;
clipboard.setData( "text/plain", data );
});
});
<button id="btnCopiar">Copiar</button>
Even if I have initialized the clipboard outside the click event, it is not working
I wonder if this has to with the synchronous way you have written the code.
Your line var data = ... implies that the variable data is receiving its information from a call to the server that only happens right at that moment. (I'm making some assumptions about code you have deleted in order to make the question more concise and understandable, though I could be wrong about that.) That data is going to take a little while to arrive. However, immediately after that line you are using the data variable in the clipBoardObj.on("copy", function(event) {... function. The first time you run that function, the data will not yet have arrived. However, some time will elapse before the user clicks the button a second time. When that happens, there may have been enough time for the first call to the server to have returned, and data will have some data. Note, however, that the second time you run that function, it will only be using the data from the first call to the server, which may or may not be acceptable.
I am triggering a change event in my casperJS script which triggers an AJAX request like such:
casper.evaluate(function(i) {
$("form:eq(2) select option:eq(" + i + ")").attr("selected", "selected").change();
},i);
How can I make casperJS wait until the underlying AJAX request has been finished? Already tried to look at the docs but I am more or less stuck. Can anyone guide me into the right direction?
You can always do this in a static way using casper.wait.
casper.thenEvaluate(function(i) {
// change()
},i).wait(5000).then(function(){
// further processing
});
And hope that the request is done in 5 seconds, but maybe you lose some time waiting when the request is done much sooner than 5 seconds. The problem is that as soon as the request is finished doesn't mean that the page is ready/changed.
Another possibility would be to wait for the request to finish, but for this to work you will need to register for the success event of the request somehow. Most of the time you don't have access to this from the global scope. If you do then you can use
casper.thenEvaluate(function(i) {
window._someUniqueVariable = false;
registerSuccessEvent(function(data){
window._someUniqueVariable = true;
});
},i).waitFor(function check(){
return this.evaluate(function(){
window._someUniqueVariable = true;
});
}, function(){
// further processing
});
A more Casper-way of doing that would be to use casper.waitForResource, but then you would need to know the url beforehand or at least able to deduce it from the page.
In the general case, when the request comes back it does something to your page. So you should be able to waitForSelector with a new element or waitForSelectorTextChange or waitUntilVisible etc.
you probably missed waitForResource
from the docs: http://casperjs.readthedocs.org/en/latest/modules/casper.html#waitforresource
casper.waitForResource("you url here", function()
{
// place your code here
});
I have the following kludgey code;
HTML
<input type="search" id="search_box" />
<div id="search_results"></div>
JS
var search_timeout,
search_xhr;
$("#search_box").bind("textchange", function(){
clearTimeout(search_timeout); search_xhr.abort();
search_term = $(this).val();
search_results = $("#search_results");
if(search_term == "") {
if(search_results.is(":visible"))
search_results.stop().hide("blind", 200);
} else {
if(search_results.is(":hidden"))
search_results.stop().show("blind", 200);
}
search_timeout = setTimeout(function () {
search_xhr = $.post("search.php", {
q: search_term
}, function(data){
search_results.html(data);
});
}, 100);
});
(uses the textchange plugin by Zurb)
The problem I had with my original more simple code was that it was horribly unresponsive. Results would appear seconds later, especially when typed slower, or when Backspace was used, etc.
I made all this, and the situation isn't much better. Requests pile up.
My original intention is to use .abort() to cancel out whatever previous request is still running as the textchange event is fired again (as per 446594). This doesn't work, as I get repeated errors like this in console;
Uncaught TypeError: Cannot call method 'abort' of undefined
How can I make .abort() work in my case?
Furthermore, is this approach the best way to fetch 'realtime' search results? Much like Facebook's search bar, which gives results as the user types, and seems to be very quick on its feet.
You'd do well to put a small delay in before sending the request. If the user hits another key within 100ms (or some other time of your choosing) of the last there is no need to send the request in the first place.
When actually sending the request you should check to see if one is already if active. If it is, cancel it.
e.g.
if (search_xhr) {
search_xhr.abort();
}
don't forget to reset that var on a successful retrieval. e.g. delete search_xhr;
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