{"id":13185,"date":"2022-10-03T07:03:01","date_gmt":"2022-10-03T01:33:01","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/"},"modified":"2022-10-03T07:03:01","modified_gmt":"2022-10-03T01:33:01","slug":"solved-why-this-mmediately-invoked-functions-is-not-working-properly","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/","title":{"rendered":"[Solved] Why this mmediately invoked functions is not working properly"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-13550293\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"13550293\" data-parentid=\"13550216\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>First off, I notice two problems:<\/p>\n<ul>\n<li>You have a syntax error in the parameter list to <code>$.post<\/code><\/li>\n<li>You probably don&#8217;t want to do this: <code>setTimeout(chatcom_load_one(id), 1000);<\/code><\/li>\n<\/ul>\n<p>Here&#8217;s an updated version of your code with these errors fixed:<\/p>\n<pre><code>function chat_com_one(id) {\n\n    $('#chatcom').show('fast');\n    (function chatcom_load_one(id) {\n        $.post('sendchat2.php', {\n            option: 'chatcom_load_one',\n            tocom: id\n        }, function (data) {\n            $('#chatcom #commid #commidwin').html(data);\n            setTimeout(function () {\n                chatcom_load_one(id);\n            }, 1000);\n        });\n    }());\n\n    $('#chatcom_send').click(function () {\n        var text = document.getElementById('chatcom_text').value;\n        $.post('sendchat2.php', {\n            option: 'chat_com_send_one',\n            text: text,\n            tocom: id\n        },\n        function (data) {\n            document.getElementById('chatcom_text').value=\"\";\n        });\n    });\n}\n<\/code><\/pre>\n<p>Also, since you&#8217;re using jQuery, you can simplify <code>document.getElementById...<\/code>. Another updated version (with some more changes to make it more readable):<\/p>\n<pre><code>function chat_com_one(id) {\n    $('#chatcom').show('fast');\n\n    (function chatcom_load_one(id) {\n        $.post('sendchat2.php', {\n            option: 'chatcom_load_one',\n            tocom: id\n        }, function (data) {\n            $('#commidwin').html(data);\n            setTimeout(function () {\n                chatcom_load_one(id);\n            }, 1000);\n        });\n    }(id));\n\n    $('#chatcom_send').click(function () {\n        var text = $('#chatcom_text').val();\n        $.post('sendchat2.php', {\n            option: 'chat_com_send_one',\n            text: text,\n            tocom: id\n        },\n        function (data) {\n            $('#chatcom_text').val('');\n        });\n    });\n}\n<\/code><\/pre>\n<p>These are just a few cleanups, there may be others.<\/p>\n<p><strong>Edit:<\/strong><\/p>\n<p>Added devnull69&#8217;s insight to my final updated code. Hopefully it&#8217;s useful (accept his answer if this was the problem).<\/p>\n<p><strong>Edit: Other notes<\/strong><\/p>\n<p>Why are you doing <code>$.post<\/code> in <code>chatcom_load_one<\/code>? It would make much more sense as a <code>$.get<\/code>, and the query parameters would still be sent. It&#8217;s not really a problem per se, but it&#8217;s bad style. This should probably be in a file called <code>getchat.php<\/code> or something instead of doing what I expect is looking for the <code>text<\/code> parameter.<\/p>\n<p>Also, I don&#8217;t know the implementation of <code>sendchat2.php<\/code>, but you should probably reduce the timeout. Try something like 250ms or so. That&#8217;s not going to overload the server and it&#8217;ll improve response time.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">7<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Why this mmediately invoked functions is not working properly <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] First off, I notice two problems: You have a syntax error in the parameter list to $.post You probably don&#8217;t want to do this: setTimeout(chatcom_load_one(id), 1000); Here&#8217;s an updated version of your code with these errors fixed: function chat_com_one(id) { $(&#8216;#chatcom&#8217;).show(&#8216;fast&#8217;); (function chatcom_load_one(id) { $.post(&#8216;sendchat2.php&#8217;, { option: &#8216;chatcom_load_one&#8217;, tocom: id }, function (data) { &#8230; <a title=\"[Solved] Why this mmediately invoked functions is not working properly\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/\" aria-label=\"More on [Solved] Why this mmediately invoked functions is not working properly\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[333,339],"class_list":["post-13185","post","type-post","status-publish","format-standard","hentry","category-solved","tag-javascript","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Why this mmediately invoked functions is not working properly - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Why this mmediately invoked functions is not working properly - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] First off, I notice two problems: You have a syntax error in the parameter list to $.post You probably don&#8217;t want to do this: setTimeout(chatcom_load_one(id), 1000); Here&#8217;s an updated version of your code with these errors fixed: function chat_com_one(id) { $(&#039;#chatcom&#039;).show(&#039;fast&#039;); (function chatcom_load_one(id) { $.post(&#039;sendchat2.php&#039;, { option: &#039;chatcom_load_one&#039;, tocom: id }, function (data) { ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-03T01:33:01+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Why this mmediately invoked functions is not working properly\",\"datePublished\":\"2022-10-03T01:33:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/\"},\"wordCount\":204,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"javascript\",\"php\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/\",\"name\":\"[Solved] Why this mmediately invoked functions is not working properly - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-03T01:33:01+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Why this mmediately invoked functions is not working properly\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Why this mmediately invoked functions is not working properly - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Why this mmediately invoked functions is not working properly - JassWeb","og_description":"[ad_1] First off, I notice two problems: You have a syntax error in the parameter list to $.post You probably don&#8217;t want to do this: setTimeout(chatcom_load_one(id), 1000); Here&#8217;s an updated version of your code with these errors fixed: function chat_com_one(id) { $('#chatcom').show('fast'); (function chatcom_load_one(id) { $.post('sendchat2.php', { option: 'chatcom_load_one', tocom: id }, function (data) { ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/","og_site_name":"JassWeb","article_published_time":"2022-10-03T01:33:01+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Why this mmediately invoked functions is not working properly","datePublished":"2022-10-03T01:33:01+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/"},"wordCount":204,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["javascript","php"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/","url":"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/","name":"[Solved] Why this mmediately invoked functions is not working properly - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-03T01:33:01+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-why-this-mmediately-invoked-functions-is-not-working-properly\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Why this mmediately invoked functions is not working properly"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/13185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=13185"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/13185\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=13185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=13185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=13185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}