سؤال

I would like to ask if there is any way to execute the same function at the same time.. see this..

function convert_points()
{
    show_loading();
    xajax_ConvertPoints();
    xajax_GetRegularGamingCards();  
}

when the xajax_ConvertPoints is called. a MILISECOND delay after the xajax_GetRegularGamingCards is called. i proved that because of the logs that i put in each function.. like this..

2013-02-07 17:13:53 || LAUNCHPAD42 || TRANSACTION TYPE: CONVERT POINTS || SWCQAZ1 ||
2013-02-07 17:13:53 || LAUNCHPAD43 || TRANSACTION TYPE: CONVERT POINTS || SWCQAZ1 ||
2013-02-07 17:13:53 || LAUNCHPAD44 || TRANSACTION TYPE: CONVERT POINTS || SWCQAZ1 ||
2013-02-07 17:13:53 || LAUNCHPAD45 || TRANSACTION TYPE: CONVERT POINTS || SWCQAZ1 ||
2013-02-07 17:13:54 || LAUNCHPAD46 || TRANSACTION TYPE: CONVERT POINTS || SWCQAZ1 ||
2013-02-07 17:13:54 || LAUNCHPAD47 || TRANSACTION TYPE: CONVERT POINTS || SWCQAZ1 || 
2013-02-07 17:13:54 || LAUNCHPAD48 || TRANSACTION TYPE: CONVERT POINTS || SWCQAZ1 ||

Theres a difference in 2013-02-07 .54 and .53 (2013-02-07 17:13:53) MILISECONDS. is there any way to called it at the exact time?

هل كانت مفيدة؟

المحلول

It's impossible to have the same line of code guaranteed to be executed at the same millisecond in any kind of language. In JavaScript you can execute a functon without waiting for it's result and continue going to the other line of execution using setTimeout

function convert_points(){
    setTimeout(show_loading,0);
    setTimeout(xajax_ConvertPoints,0);
    setTimeout(xajax_GetRegularGamingCards,0);  
}

نصائح أخرى

When your Javascript code is executed it can only happen from one thread at a time, so it's not possible to execute these two function at the exact same time since these are executed sequentially.

Javascript operates in a single thread, so in any scenario they would be run sequentially.

What makes you require them to be executed at the same time? Why not join them into one ajax request by joining the two scripts together?

Another option is to delay the execution of the script you're calling by putting a sleep in it or something. But I really would not recommend this. The best I would suggest is to make one script do what you need instead of both.

If it is a DB operation that requires data simultaneity, you could use transact style commands to ensure that there isn't a nanosecond where there are data discrepancies. This is better because for whatever reason you need them to be called simultaneously, one could alter javascript on the client end very easily from an end user point of view. This is why its better to do it on the server.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top