{"id":922,"date":"2017-04-22T19:56:35","date_gmt":"2017-04-23T03:56:35","guid":{"rendered":"http:\/\/fox-gieg.com\/tutorials\/?p=922"},"modified":"2020-06-25T09:31:25","modified_gmt":"2020-06-25T17:31:25","slug":"javascript-in-maxmspjitter","status":"publish","type":"post","link":"https:\/\/fox-gieg.com\/tutorials\/2017\/javascript-in-maxmspjitter\/","title":{"rendered":"JavaScript in Max\/MSP\/Jitter"},"content":{"rendered":"<pre>\r\n\"use strict\";\r\n\r\n\/\/ ~ ~ ~ these variables are built-in ~ ~ ~\r\ninlets = 3;\r\noutlets = 3;\r\n\/\/ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\r\n\r\nvar nowTime = 0;\r\nvar lastTime = 0;\r\nvar deltaTime = 0;\r\nvar timeCounter = 0;\r\nvar timeMax = 10;\r\nvar firstRun = true;\r\n\r\n\/\/ ~ ~ ~ these functions are built-in ~ ~ ~\r\n\r\n\/\/ this runs when the object is first loaded\r\nfunction loadbang() {\r\n    init();\r\n}\r\n\r\n\/\/ this runs each time the object receives a bang\r\nfunction bang() {\r\n    if (inlet === 0) {\r\n        update();\r\n    } else if (inlet === 2) {\r\n        init();\r\n    }\r\n}\r\n\r\n\/\/ this runs when the object receives a float message\r\nfunction msg_float(data) {\r\n    if (inlet === 1) {\r\n        timeMax = data;\r\n        outlet(1, timeMax);\r\n        post(\"new max: \" + timeMax.toFixed(2));\r\n    }\r\n}\r\n\r\n\/\/ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\r\n\r\nfunction init() {\r\n    if (firstRun && typeof jsarguments[1] === \"number\") {\r\n        timeMax = jsarguments[1];\r\n        firstRun = false;\r\n    }\r\n    nowTime = new Date().getTime();\r\n    timeCounter = 0;\r\n    outlet(0, timeCounter);\r\n    outlet(1, timeMax);\r\n    outlet(2, bang);\r\n    post(\"reset\");\r\n}\r\n\r\nfunction update() {\r\n    lastTime = nowTime;\r\n    nowTime = new Date().getTime();\r\n    deltaTime = (nowTime - lastTime) \/ 1000.0;\r\n    timeCounter += deltaTime;\r\n    if (timeCounter < timeMax) {\r\n        outlet(0, timeCounter);\r\n        post(timeCounter);\r\n    } else {\r\n        init();\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;use strict&#8221;; \/\/ ~ ~ ~ these variables are built-in ~ ~ ~ inlets = 3; outlets = 3; \/\/ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ var nowTime = 0; var lastTime = 0; var deltaTime = 0; var timeCounter = [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[25],"tags":[],"_links":{"self":[{"href":"https:\/\/fox-gieg.com\/tutorials\/wp-json\/wp\/v2\/posts\/922"}],"collection":[{"href":"https:\/\/fox-gieg.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fox-gieg.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fox-gieg.com\/tutorials\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/fox-gieg.com\/tutorials\/wp-json\/wp\/v2\/comments?post=922"}],"version-history":[{"count":4,"href":"https:\/\/fox-gieg.com\/tutorials\/wp-json\/wp\/v2\/posts\/922\/revisions"}],"predecessor-version":[{"id":1034,"href":"https:\/\/fox-gieg.com\/tutorials\/wp-json\/wp\/v2\/posts\/922\/revisions\/1034"}],"wp:attachment":[{"href":"https:\/\/fox-gieg.com\/tutorials\/wp-json\/wp\/v2\/media?parent=922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fox-gieg.com\/tutorials\/wp-json\/wp\/v2\/categories?post=922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fox-gieg.com\/tutorials\/wp-json\/wp\/v2\/tags?post=922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}