First of all i am not good enough in js.
I have two function in a page and i have to call another function in an existing function.
I have=>
function suggest1(inputString){
if(inputString.length == 0) {
$('#suggestions1').fadeOut();
}
else{
$('#p_name').addClass('load');
$.post("autosuggest.php", {queryString: ""+inputString+""}, function(data)
{
if(data.length >0)
{
$('#suggestions1').fadeIn();
$('#suggestionsList1').html(data);
$('#p_name').removeClass('load');
}
});
}
}
function fill1(thisValue) {
$('#p_name').val(thisValue);
$('#p_nameA').val(thisValue);
setTimeout("$('#suggestions1').fadeOut();", 100);
}
This is an auto suggest function
Ineed to call function which is bellow, which can use the value of $('#p_name').val(thisValue); in var name_tast. Is it possible or not?
function getAccaounce()
{
var name_tast = document.getElementById("p_nameA").value;
$.ajax({
type: "POST",
url: 'ajax_accaounce.php',
dataType: 'json',
data: {
'p_nameA': name_tast,
},
success: function(data)
{
$('#available1').html(data.message1);
$('#price1').html(data.message2);
}
});
}
Thanks in advance.
Related
I have this code :
.on('finish.countdown', function() {
var onEndAuction = function () {
$.ajax({
type: "POST",
url: "{{path('app_auction_end')}}",
data: {auctionId:{{ aReturn.oAuction.getId()}}},
success: function (data) {
console.log(data);
if (data == 0) {
setTimeout(onEndAuction, i_timer);
} else {
document.location.reload(true);
}
}
});
};
});
I want if data == 0 need to make another call on app_auction_end after 10 sec. Can you help me please ? Thx in advance and sorry for my english
Give the operation a named function:
var someFunction = function () {
$.ajax({
//...
});
};
Which you would then use for your .on() call:
.on('finish.countdown', someFunction)
And in the success handler, set a timeout for that function:
if (data == 0) {
setTimeout(someFunction, i_timer);
}
.on('finish.countdown', function() {
var onEndAuction = function () {
$.ajax({
type: "POST",
url: "{{path('app_auction_end')}}",
data: {auctionId:{{ aReturn.oAuction.getId()}}},
success: function (data) {
console.log(data);
if (data == 0) {
setTimeout(onEndAuction, i_timer);
} else {
document.location.reload(true);
}
}
});
};
//do our initial call otherwise it will never get called.
onEndAuction();
});
I have some code on a file that makes Ajax calls. This file is being called as a function by multiple other files that creates a new instance each time.
This is the JS code that is being called:
define(["underscore", "homeop", "domReady!"],
function (_, homeop, domready) {
var timeout = 500;
return function (opUrl, opList, onCallback) {
// IRRELEVANT CODE
var getFetch = function (optionName) {
$.ajax({
url: optionsUrl,
data: { optionNames: [optionName] },
type: "POST",
dataType: "json",
async: false,
traditional: true,
success: function (data) {
_.each(data, function (optionData, optionName) {
if (homeop.globalCache[optionName] === null) {
homeop.globalCache[optionName] = optionData;
}
});
},
error: function (message) {
console.error(message.responseText);
}
});
};
self.getInfo = function (optionName) {
if (homeop.globalCache[optionName] === undefined) {
if (!_.contains(homeop.getOption(), optionName)) {
getFetch(optionName);
}
// MORE IRRELEVANT CODE GOES HERE
In other JS files, I call the get function; for example
var these = new getOptions(optionsUrl, optionsList, onLoadCallback);
var getOpt = these.get(OptionsUrl);
The problem is I am making multiple calls to the get information from the database causing multiple call to my JS file. Each new instance of the JS file will create a ajax call.
Is there a way to wait for all the calls to be done and then get data from the database? In other words how can I somehow combine all the call to my 'getOption.js'?
Thanks
Try this.. You can also implement queue in place of stack
var optionStack = [];
var isAvailable = true;
var getFetch = function (optionName) {
if(isAvailable){
isAvilable = false; // function not available now
}
else {
optionStack.push(optionName)
return;
}
$.ajax({
url: optionsUrl,
data: { optionNames: [optionName] },
type: "POST",
dataType: "json",
async: false,
traditional: true,
success: function (data) {
_.each(data, function (optionData, optionName) {
if (homeop.globalCache[optionName] === null) {
homeop.globalCache[optionName] = optionData;
}
});
},
error: function (message) {
console.error(message.responseText);
},
done: function (){
isAvailable = true;
if(optionStack.length > 0){
getFetch(optionStack.pop());
}
}
});
};
I want to return true or false after making this ajax call:
function write_csv(data, path, file) {
$.ajax({
url: 'functions.php',
type: 'POST',
data: {
operation: 'SAVE_CSV',
save_path: path,
save_file: file,
save_string: data
},
success: function(result) {
console.log('write_file(' + path + file + '); -done');
return true; /* <-- */
}
});
}
Example use case of what I want:
function make_csv () {
/*
|
V
*/
if (write_csv(my_data, my_path, 'export.csv') == true) {
go_on();
}
function go_on() {
alert('YEAH!');
}
}
I know it's async, but maybe someone has another idea.
I won't do it with if's and stuff...
You can use Promises or callbacks to accomplish what you want.
function write_csv(data, path, file, callback) {
$.ajax({
url: 'functions.php',
type: 'POST',
data: {
operation: 'SAVE_CSV',
save_path: path,
save_file: file,
save_string: data
},
success: function(result) {
console.log('write_file(' + path + file + '); -done');
callback(true); /* <-- */
}
});
}
And:
function make_csv () {
/*
|
V
*/
function go_on() {
alert('YEAH!');
}
write_csv(my_data, my_path, 'export.csv', function(result) {
if (result == true) {
go_on();
}
});
}
I am going to go off of jQuery convention and give you a "jQuery" answer since that's what you are using.
In jQuery, you have the ability to pass in a callback (a function to "call" once the actual function you are using finishes) in most jQuery methods. jQuery's convention is to have the callback as the last parameter you pass in into a function. In your example, your write_csv() function would look like this with an extra callback as the last parameter:
function write_csv(data, path, file, callback){
$.ajax({
url: 'functions.php',
type: 'POST',
data: {
operation: 'SAVE_CSV',
save_path: path,
save_file: file,
save_string: data
},
success: function(result) {
console.log('write_file(' + path + file + '); -done');
callback(true);
}
error: function(result){
console.log('async failed');
callback(false);
}
});
}
Notice the error key being passed in and the changes made to the success key in the $.ajax() function.
Now, when you want to use your async function in an if conditional statement, you can use
write_csv(my_data, my_path, 'export.csv', function(response){
if(response === true){
go_on()
}
else if(response === false){
// do something else
}
});
I have dilaog box which loads data in success function of an ajax call as follows.
$.ajax({
url: '#Url.Action("GetPolicyPremiumAllocation", "Policy")',
data: { policyID: selPolicyId },
type: 'POST',
success: function (data) {
if (data.length > 0) {
alert(data);
document.getElementById("modal_dialog").innerHTML = "";
// $("#modal_dialog").empty();
$("#modal_dialog").load(data,function( ) {
$("#close-button-id").on("click", CloseDialog);
});
$("#modal_dialog").dialog("open");
}
}
});
First time the div of dilaog is loading correct data.But Second time,it just shows the old data. I have tried to clear cache in the index action of my controller.Unable to figure out how to solve this.Please help.
Set ajax cache option to false:
$.ajax({
url: '#Url.Action("GetPolicyPremiumAllocation", "Policy")',
data: { policyID: selPolicyId },
cache:false,
type: 'POST',
success: function (data) {
if (data.length > 0) {
alert(data);
document.getElementById("modal_dialog").innerHTML = "";
// $("#modal_dialog").empty();
$("#modal_dialog").load(data,function( ) {
$("#close-button-id").on("click", CloseDialog);
});
$("#modal_dialog").dialog("open");
}
}
});
Or decorate your Action with [OutputCache(NoStore = true, Duration = 0)]
I have made a function that is controlling a row in a my database for a certain number with AJAX.
Im calling the function with a click function and putting the function in a setInterval function to make the check 10 times a second.
In the beginning it will return 0, but at some point (usually within 5 seconds) it will return something els than 0, when it does i want to clearInterval.
But im not sure how to this?
This is my function:
function get_buzzer() {
$.ajax({
url: 'ajax_buzzer.php',
dataType: 'json',
async: false,
type: 'post',
data: {
job: 'get'
},
success:function(s) {
if(s['number'] == 0) {
var player = false;
} else {
var player = true;
}
}, error:function(e) {
}
});
}
$(document).ready(function() {
$('#test').click(function() {
var buzzer = setInterval("get_buzzer()",100);
});
});
You can do something like
$(document).ready(function () {
//make buzzer a share variable
var buzzer;
$('#test').click(function () {
buzzer = setInterval(get_buzzer, 100);
});
function get_buzzer() {
$.ajax({
url: 'ajax_buzzer.php',
dataType: 'json',
async: false,
type: 'post',
data: {
job: 'get'
},
success: function (s) {
if (s['number'] != 0) {
//if number is not 0 then clear the interval
clearInterval(buzzer)
}
},
error: function (e) {}
});
}
});
Try this : declare global variable to store interval and call window.clearInterval in success call of ajax
var buzzer;
function get_buzzer() {
$.ajax({
url: 'ajax_buzzer.php',
dataType: 'json',
async: false,
type: 'post',
data: {
job: 'get'
},
success:function(s) {
if(s['number'] == 0) {
var player = false;
} else {
var player = true;
//clear interval
window.clearInterval(buzzer);
}
}, error:function(e) {
}
});
}
$(document).ready(function() {
$('#test').click(function() {
buzzer = setInterval("get_buzzer()",100);
});
});
Use:
inside success use: And make var buzzer Gloval var.
clearInterval(buzzer);
Refence
You just need to clear the interval in the success handler of ajax call over a condition.
success: function (s) {
if (s['number'] != 0) {
//if number is not 0 then clear the interval
clearInterval(buzzer)
}
},
error: function (e) {}